a question about deleting records from a DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

a question about deleting records from a DB

Post 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
cogentconcept
Forum Newbie
Posts: 3
Joined: Sun Dec 26, 2010 2:43 am

Re: a question about deleting records from a DB

Post 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
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

Re: a question about deleting records from a DB

Post by Tokunbo »

@Trent
No, my application does not use datasets to store data,

what is Table Adapters?
Post Reply