Jump to content

Trying to delete a record


stonelord

Recommended Posts

Hi, I am new to programming in php and trying to learn by creating my own database interactive website. I cannot get my delete button to delete each record, it says that it did! but it doesn't. Here is my link that I created to create a button for every record that appears from the database;

	echo '<tr><td>';
echo $row['Department'];
echo '</td><td>';
echo $row['Name'];
echo '</td><td>';
echo $row['Phone'];
echo '</td><td>';
echo $row['DC'];
echo '</td><td>';
echo $row['Email'];
echo '</td><td>';
echo $row['Notes'];
echo '</td><td>';
echo '<form method="post" action="delete_contact.php">';
echo '<input type="submit" name="delete" value="Delete Record">';
echo '</form>';
echo '</td></tr>'; 

 

Now this is the code for my delete_contact.php file;

<? 

$id=$_POST['delete']; 
//connect to DB and insert data
mysql_connect("localhost", "my_db", "db") or die('I cannot connect to data base because: '.mysql_error()); 
mysql_select_db("my_db") or die(mysql_error()); 
mysql_query("DELETE FROM `ci_testing` WHERE $id"); 
Print "Your information has been successfully deleted from the database."; 
?> 

 

Any help will be greatly appreciate it! Thank you

Link to comment
https://forums.phpfreaks.com/topic/110681-trying-to-delete-a-record/
Share on other sites

it looks like u are just deleteing the phrase "delete"

 

u would have to have somthing to id the row and such that u are deleting unless u are just tring to show what u are doing

?

from a code i have been writing:

$sql = "DELETE FROM lslstore WHERE vname = '$key' AND agent_id = '$owner_id'";
        $result = mysql_query($sql) or die(mysql_error());
        echo 'Deleted...';

 

right now ur code $id = "delete"....

is that the whole code or part of it?

 

and do u have records in there.... what you should do.... is make it like this use that same form ... but also if u have this printing out each row in a loop i would add that to the loop and for name... put that ID = to a value ... Example:

 

echo "<table border='1'>";
echo "<tr><th>Name</th><th>UUID</th><th>Distance</th><th>Time</th><th>sensor</th><th>SIM</th><th>Owner</th><th>delete info</tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td><center>"; 
echo $row[vname];
echo "</center></td><td>"; 
echo $row[vuuid];
echo "</td><td>";
echo $row[vdist];
echo "</td><td>"; 	
echo $row[vtime];
echo "</td><td>"; 
echo $row[vsensor];
echo "</td><td>"; 
echo $row[vsim];
echo "</td><td>"; 
echo $row[vowner];
        echo "</td><td>"; 
echo '<form method="post" action="delete_contact.php">';
echo '<input type="submit" name="delete" value=$row[vname]>';
echo '</form>';
echo "</td></tr>"; 
} 

	echo "</table><center><br><br>";
	echo 'Created for us with Second Life<br>Saladin NightFire<br>Wolf Creations';
	echo "</center>";

OK, I GOT IT !!! ufff!!! man that was hard (at least for me). I don't understand it completely but it works great!;

	echo '</td><td>';
echo '<a href="delete_contact.php?id='.$row['id'].'">Delete Record</a>';
echo '</td></tr>'; 

 

And this is my delete_contact.php file code;

<?PHP
//connect to DB and insert data
mysql_connect("localhost", "my_db", "db") or die('I cannot connect to data base because: '.mysql_error()); 
mysql_select_db("ci_testing") or die(mysql_error()); 

$id=$_GET['id']; 
mysql_query("DELETE FROM `PhoneList` WHERE id='$id'") OR die(mysql_error());
echo "Your information has been successfully deleted from the database"; 
mysql_close()
?>

 

Thank you for your help, you forced me to think and hope to become a good programmer so that I too can help people out! thanks again

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.