Deleting Entry From MySQL Table

Reply

Join Date: May 2009
Posts: 1
Reputation: onethirtyone is an unknown quantity at this point 
Solved Threads: 0
onethirtyone onethirtyone is offline Offline
Newbie Poster

Deleting Entry From MySQL Table

 
0
  #1
May 7th, 2009
Hey all this is a pretty simple question, basically I connect to a mysql database, and iterate a list of names like this:

  1. $result = mysql_query("SELECT * FROM Students");
  2.  
  3. echo "<table border=\"1\">";
  4.  
  5. while($row = mysql_fetch_array($result))
  6. {
  7. print "
  8. <tr>
  9. <td>
  10. "
  11. . $row['Student'] .
  12. "
  13. </td>
  14. <td>
  15. <form name=\"FRMdelete" . $row['Student'] . "\" action=\"deletestudent.php\" method=\"POST\" style=\"margin-bottom:0;\">
  16. <input name=\"delete\" type=\"submit\" id=\"" . $row['Student'] . "\" value=\"Delete\" OnClick=\"return confirm('Are you sure you want to Delete " . $row['Student'] . "?')\">
  17. </form>
  18. </td>
  19. </tr>
  20. ";
  21. }
  22. echo"</table>";
  23. mysql_close($con);

Then I using POST, I pass the name of the student from the ID of the input to deletestudent.php, which looks like this:

  1. <?php
  2. $studentremoved = $_POST['delete'];
  3. $sql = "DELETE FROM Students WHERE Student='" . $studentremoved . "'";
  4. $con = mysql_connect("localhost","root","password");
  5. if (!$con)
  6. {
  7. die('Could not connect: ' . mysql_error());
  8. }
  9. mysql_query($sql);
  10. mysql_close($con);
  11. header("Location: ".$_SERVER['HTTP_REFERER']);
  12. ?>

For some reason the scripts execute without throwing and errors to my debugger, but it doesn't delete the record =/

What am I missing, I know its probably obvious. Sorry for the sloppy code, I am a beginner =/
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 93
Reputation: humbug is an unknown quantity at this point 
Solved Threads: 13
humbug's Avatar
humbug humbug is offline Offline
Junior Poster in Training

Re: Deleting Entry From MySQL Table

 
0
  #2
May 7th, 2009
Your form should look like this:
  1. <form ...>
  2. <input type="hidden" name="student" value="$row['student']" />
  3. <input type="submit" name="delete" value="Delete" />
  4. </form>

Then you can get which student to delete on deletestudent.php via $_POST['student']. (you can use isset($_POST['delete']) to see if they clicked "Delete" but $_POST['delete'] will hold the value "Delete").

The id attribute in html is used for javascript or CSS to select that particular element.
Last edited by humbug; May 7th, 2009 at 4:05 am. Reason: premature post :P
"If your not having fun, your doing something wrong." - Humbug
★ Did I help you out? Did I piss you off? Add to my reputation!
The Gabriel Method is a great book for losing weight and keeping healthy - I know Jon Gabriel Personally.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 136
Reputation: justted is an unknown quantity at this point 
Solved Threads: 2
justted justted is offline Offline
Junior Poster

Re: Deleting Entry From MySQL Table

 
0
  #3
May 8th, 2009
Something simple but have you given your database user permissions to delete?

Im also an old newbie and still trying to get my head round it all. lol
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 93
Reputation: humbug is an unknown quantity at this point 
Solved Threads: 13
humbug's Avatar
humbug humbug is offline Offline
Junior Poster in Training

Re: Deleting Entry From MySQL Table

 
0
  #4
May 9th, 2009
Might also be a good idea to do
  1. if (!mysql_query($sql)){
  2. die (mysql_error());
  3. }
"If your not having fun, your doing something wrong." - Humbug
★ Did I help you out? Did I piss you off? Add to my reputation!
The Gabriel Method is a great book for losing weight and keeping healthy - I know Jon Gabriel Personally.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC