943,955 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1187
  • PHP RSS
May 7th, 2009
0

Deleting Entry From MySQL Table

Expand Post »
Hey all this is a pretty simple question, basically I connect to a mysql database, and iterate a list of names like this:

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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 =/
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
onethirtyone is offline Offline
1 posts
since May 2009
May 7th, 2009
0

Re: Deleting Entry From MySQL Table

Your form should look like this:
html Syntax (Toggle Plain Text)
  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
Reputation Points: 20
Solved Threads: 13
Junior Poster in Training
humbug is offline Offline
93 posts
since Oct 2005
May 8th, 2009
0

Re: Deleting Entry From MySQL Table

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
Reputation Points: 10
Solved Threads: 2
Junior Poster
justted is offline Offline
140 posts
since Dec 2007
May 9th, 2009
0

Re: Deleting Entry From MySQL Table

Might also be a good idea to do
php Syntax (Toggle Plain Text)
  1. if (!mysql_query($sql)){
  2. die (mysql_error());
  3. }
Reputation Points: 20
Solved Threads: 13
Junior Poster in Training
humbug is offline Offline
93 posts
since Oct 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Combine Two RSS Feeds to Appear as One in PHP
Next Thread in PHP Forum Timeline: variable in URL called from database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC