PHP Help

Status
Not open for further replies.

xbeto78

New Member
Messages
5
Reaction score
0
Points
0
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();
?>
 

adrenlinerush

New Member
Messages
379
Reaction score
1
Points
0
have you tried google? I'm not a php guy but I think you have a different submit value for each button
 

SyncViews

New Member
Messages
37
Reaction score
0
Points
0
the value field for type="submit" is just the text to display.
Not sure what the name="..." is doing for a submit type though...I'll check that later I'm pretty sure it'd not needed at all...

Well this should do what you want.
The form:
Code:
<table width="100%">
 <!--UPDATE-->
 <form action="edit_table.php" method="post" />
  <input type="hidden" name="v_id" value="'.$id.'" /><!--hidden fields are best as not being a part of a table-->
  <input type="hidden" name="type" value="0" /> <!--type = 0 = submit :) -->
  
  <tr><td><p>Video ID:</p></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" />
  <!--END UPDATE-->
 </form>
 <!--DELETE-->
 <form action="edit_table.php" method="post" />
  <input type="hidden" name="v_id" value="'.$id.'" /><!--and we need the hidden stuff again-->
  <input type="hidden" name="type" value="1" /><!--type = 1 = delete-->
  <tr><td><input type="submit" name="delete" value="Delete" id="del" /></td></tr>
  <!--END DELETE-->
 </form>
</table>
And how to read it:
Code:
if(isset($_POST['type']))
{
	//update
	if ($_POST['type'] == 0)
	{
		$id     = $_POST['v_id'];
		$name   = $_POST['a_name'];
		$v_name = $_POST['s_name'];
		$v_url  = $_POST['url'];
		$a_img  = $_POST['img'];
		$a_site = $_POST['website'];
		//You should but some validation stuff here to prevent sql insertion attacks!
		mysql_query("UPDATE videos SET artist_name='$name', song_name='$v_name', url='$v_url', img='$a_img', site='$a_site' WHERE video_id='$id'") or exit (mysql_error());
		echo 'Video updated!';
	}
	//Delete
	else if ($_POST['type'] == 1)
	{
		$id = $_POST['v_id'];
		//You should check that the $id var is clean to prevent sql insertion attacks!
		mysql_query("DELETE FROM videos WHERE video_id='$id'") or exit (mysql_error());
		echo 'Video with ID "' . $id . '" deleted.';
	}
	//error
	else exit ('ERROR: Invalid type:' . $_POST['type'] . '!');
}
 
Last edited:

jptosso

New Member
Messages
200
Reaction score
0
Points
0
PHP:
if(isset($_POST))
{
DEFINE("_ERROR", "error, invalid ". $_POST['type']." .";
	//update
	if ($_POST['type'] == 0)
	{
		$id     = $_POST['v_id'];
		$name   = $_POST['a_name'];
		$v_name = $_POST['s_name'];
		$v_url  = $_POST['url'];
		$a_img  = $_POST['img'];
		$a_site = $_POST['website'];
		//You should but some validation stuff here to prevent sql insertion attacks!
		mysql_query("UPDATE videos SET artist_name='$name', song_name='$v_name', url='$v_url', img='$a_img', site='$a_site' WHERE video_id='$id'") or exit (mysql_error());
		echo 'Video updated!';
	}
	//Delete
	elseif ($_POST['type'] == 1)
	{
		$id = $_POST['v_id'];
		//You should check that the $id var is clean to prevent sql insertion attacks!
		mysql_query("DELETE FROM videos WHERE video_id='$id'") or exit (mysql_error());
		echo 'Video with ID "' . $id . '" deleted.';
	}
	//error
	elseif($_POST['type']==""){ echo _ERROR; }
}

its better ;)
 

SyncViews

New Member
Messages
37
Reaction score
0
Points
0
Hows the last elseif better than my else? The point of useing else is it returns an error if there any mistake (eg type=3) but yours only pick up empty strings?

As for the message it's self I don't really see any disadvantagse or advantages of either method. It's not like your calling (_ERROR) lots of times or anything.
 

mitamata

New Member
Messages
81
Reaction score
0
Points
0
Not sure if you've figured it out yet, but in case you still need it...


Pressing this button:

HTML:
<input type="submit" name="pressed_button" value="Press me">


adds the following to the post variable:

PHP:
$_POST['pressed_button'] = "Press me"

To check if that button was pressed, you could do something like:

PHP:
if ($_POST['pressed_button']!= '')
...



Basic stuff, but from what I understood that was the question :)
 

xbeto78

New Member
Messages
5
Reaction score
0
Points
0
Thanks alot guys, I tried searching for the answer before posting but I turned nothing.

It makes sense now. :lol:
 
Status
Not open for further replies.
Top