a question about deleting records from a DB
Posted: Fri Oct 28, 2011 9:04 am
hello sirs,
question#1 is about an "empty form" => submit button which I want to use to delete records in my DB. Below code(home.php) is from a loop to display the contents of the DB. I want a submit button to be displayed on the last column for every row of data, to be used to delete the particular record(row). Hence my below code:
Is my above method correct? programmer-wise?
and my deleteRecordButton code is:
question: how do I pass the id/ $rowid value from (home.php) to (deleteRecordButton.php) through the form considering that the my submit button form has no input value to be stored as a variable?
Is there a better way to do the above?
thanks
Tokunbo
question#1 is about an "empty form" => submit button which I want to use to delete records in my DB. Below code(home.php) is from a loop to display the contents of the DB. I want a submit button to be displayed on the last column for every row of data, to be used to delete the particular record(row). Hence my below code:
Code: Select all
<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $rowid; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $field1name; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $field2name; ?></font></td>
<td>
<form action="deleteRecordButton.php" method="post">
<input type="submit" />
</form>
</td>
</tr>
and my deleteRecordButton code is:
Code: Select all
<?php
$id=$_POST['id'];
$con = mysql_connect("127.0.0.1","abc","def")
or die ('ERR: Could not connect to server. '.mysql_error());
mysql_select_db('my_db',$con)
or die ('ERR: Could not use database. '.mysql_error());
mysql_query("DELETE FROM contacts WHERE mytable=$id");
mysql_close($con);
?>
Is there a better way to do the above?
thanks
Tokunbo