Page 1 of 1

a question about deleting records from a DB

Posted: Fri Oct 28, 2011 9:04 am
by Tokunbo
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:

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>
Is my above method correct? programmer-wise?

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);

?>

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

Re: a question about deleting records from a DB

Posted: Fri Oct 28, 2011 9:25 am
by cogentconcept
You will need a hidden form field to hold your $id as a value.
<type='hidden' name="id" "<?php echo $id; ?> "/>
enjoy 9ja 4eva

Re: a question about deleting records from a DB

Posted: Mon Oct 31, 2011 12:46 pm
by Tokunbo
@Trent
No, my application does not use datasets to store data,

what is Table Adapters?