Hello, im trying to build a form that lets the user update an entry in a database or delete it. I'm having trouble how to identify which button was pushed, the upate or delete?
Code:
<?php
$id = $_GET['id'];
require_once('../db_connect.php');
if(!isset($_POST['submitted'])) {
$query = "SELECT * FROM videos WHERE video_id = '$id'" ;
$result = mysql_query($query);
$v_id = mysql_result($result, 0, 'video_id');
$name = mysql_result($result, 0, 'artist_name');
$v_name = mysql_result($result, 0,'song_name');
$v_url = mysql_result($result, 0, 'url');
$a_img = mysql_result($result, 0, 'img');
$a_site = mysql_result($result, 0, 'site');
echo '
<form action="edit_table.php" method="post">
<table width="100%">
<tr><td><p>Video ID:</p></td></tr>
<tr><td><input type="hidden" name="v_id" value="'.$id.'" /></td></tr>
<tr><td><p>Artist Name:</p></td></tr>
<tr><td><input type="text" name="a_name" size="20" value="'.$name.'" /></td></tr>
<tr><td><p>Song Name:</p></td></tr>
<tr><td><input type="text" name="s_name" size="20" value="'.$v_name.'"/></td></tr>
<tr><td><p>URL:</p></td><tr>
<tr><td><textarea name="url" rows="5" cols="48" value="'.$v_url.'"></textarea></td></tr>
<tr><td><p>Image (optional):</p><td></tr>
<tr><td><textarea name="img" rows="5" cols="48" value="'.$a_img.'"></textarea></td></tr>
<tr><td><p>Website (optional):</p></td><tr>
<tr><td><input type="text" name="website" size="20" value="'.$a_site.'"/></td></tr>
<tr><td><input type="submit" name="update" value="Update" id="sub" />
<input type="submit" name="delete" value="Delete" id="del" /></td></tr>
<tr><td><input type="hidden" name="submitted" value="TRUE" /></td></tr>
</table>
</form>
';
mysql_free_result($result);
}
else {
if(isset($_POST['submitted'])) {
$vid_id = $_REQUEST['v_id'];
$new_name = $_REQUEST['a_name'];
$new_v_name = $_REQUEST['s_name'];
$new_v_url = $_REQUEST['url'];
$new_a_img = $_REQUEST['img'];
$new_a_site = $_REQUEST['website'];
$query = "UPDATE videos SET artist_name='$new_name', song_name='$new_v_name', url='$new_v_url', img='$new_a_img', site='$new_a_site' WHERE video_id='$vid_id'";
$result = @mysql_query($query);
if($result) {
echo 'The record was updated!';
}
else {
echo 'The record was not updated!';
}
}
else {
echo 'Not submitted!';
}
}
mysql_close();
?>