| | |
Deleting Entry From MySQL Table
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: May 2009
Posts: 1
Reputation:
Solved Threads: 0
Hey all this is a pretty simple question, basically I connect to a mysql database, and iterate a list of names like this:
Then I using POST, I pass the name of the student from the ID of the input to deletestudent.php, which looks like this:
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 =/
PHP Syntax (Toggle Plain Text)
$result = mysql_query("SELECT * FROM Students"); echo "<table border=\"1\">"; while($row = mysql_fetch_array($result)) { print " <tr> <td> " . $row['Student'] . " </td> <td> <form name=\"FRMdelete" . $row['Student'] . "\" action=\"deletestudent.php\" method=\"POST\" style=\"margin-bottom:0;\"> <input name=\"delete\" type=\"submit\" id=\"" . $row['Student'] . "\" value=\"Delete\" OnClick=\"return confirm('Are you sure you want to Delete " . $row['Student'] . "?')\"> </form> </td> </tr> "; } echo"</table>"; 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)
<?php $studentremoved = $_POST['delete']; $sql = "DELETE FROM Students WHERE Student='" . $studentremoved . "'"; $con = mysql_connect("localhost","root","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_query($sql); mysql_close($con); header("Location: ".$_SERVER['HTTP_REFERER']); ?>
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 =/
Your form should look like this:
Then you can get which student to delete on deletestudent.php via $_POST['student']. (you can use
The id attribute in html is used for javascript or CSS to select that particular element.
html Syntax (Toggle Plain Text)
<form ...> <input type="hidden" name="student" value="$row['student']" /> <input type="submit" name="delete" value="Delete" /> </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.
★ 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.
Might also be a good idea to do
php Syntax (Toggle Plain Text)
if (!mysql_query($sql)){ die (mysql_error()); }
★ "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.
★ 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.
![]() |
Similar Threads
Other Threads in the PHP Forum
- Previous Thread: Combine Two RSS Feeds to Appear as One in PHP
- Next Thread: Exclamation points at odd places in php email.
| Thread Tools | Search this Thread |
.htaccess ajax alerts apache api array autocomplete beginner binary broken cakephp checkbox class cms code convert cron curl database date display dynamic echo email emptydisplayvalue error errors explodefunction file files folder form forms function functions google hack href htaccess html htmlspecialchars image include insert integration ip java javascript joomla keywords limit link login loop mail menu methods mlm multiple mysql network object oop paypal pdf php problem query radio random recursion recursive redirect regex remote script search securephp server sessions shot sms soap source space sql subscription syntax system table tutorial update upload url validator variable video web xml youtube





