<? $con = mysql_connect("mysql","xxx","xxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("inventory", $con); if(!isset($cmd)) { $result = mysql_query("SELECT * FROM main"); while($r=mysql_fetch_array($result)) { $text=$r["text"]; echo "<a href='delete.php?cmd=delete&text=$text'>$text - Delete</a>"; echo "<br>"; } } ?> <? if($_GET["cmd"]=="delete") { $sql = "DELETE FROM main"; $result = mysql_query($sql); echo "Row deleted!"; } mysql_close($con); ?>
PHP delete from mysql database
Started by
outatime
, Mar 16 2010 08:27 PM
#1
Posted 16 March 2010 - 08:27 PM
#2
Posted 19 March 2010 - 05:36 AM
Pretty simple solution with the code you provided you are telling php to empty the whole table called main. The issue with the way you got it IMO is 2 things, first you are using a column called text. If it is just that text this is a bad way to delete items since if you have a duplicate entry it will also be deleted (something that not everyone may want to happen).
Here is something to consider, if you have an ID tag or a auto-increment primary key use that for the delete code.
Here is something to consider, if you have an ID tag or a auto-increment primary key use that for the delete code.
<? $con = mysql_connect("mysql","xxx","xxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("inventory", $con); if(!isset($cmd)) { $result = mysql_query("SELECT * FROM main"); while($r=mysql_fetch_array($result)) { $text=$r["text"]; $id=$r['id']; // Use if you have an auto-increment field echo "<a href='delete.php?cmd=delete&text=$id'>$text - Delete</a>"; echo "<br>"; } } // Also there is no reason to close and reopen the php tag unless you inputting html directly that will always show. if($_GET["cmd"]=="delete") { $text = $_GET['text']; $sql = "DELETE FROM main WHERE id=$text"; // replace with column name to use as the identifier. ie text this way you only delete that row. $result = mysql_query($sql); echo "Row deleted!"; } mysql_close($con); ?>
Edited by amw_drizz, 19 March 2010 - 05:38 AM.
#3
Posted 30 March 2010 - 07:43 PM
Thanks! Sorry it took so long to get back. But that's exactly what I needed.
Similar Topics
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users