joshgarrod Posted December 5, 2008 Share Posted December 5, 2008 hiy guys, I i am having issues with my delete script. it just reloads the page, changes the id in the url but it doesnt remove it? any ideas? thanks in advance: <?php $con = mysql_connect("host","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db", $con); $query = "SELECT * FROM `awnings`"; $result = mysql_query($query); while($row = mysql_fetch_row($result)) { echo("<table border='1' wIDth='100%' align='center'>\n"); echo("<tr>\n"); print("<td width='19%' align=center><em>Make: </em>$row[1]</td>\n"); print("<td width='19%'align=center><em>Model: </em>$row[2]</td>\n"); print("<td width='19%'align=center><em>Size: </em>$row[3]</td>\n"); print("<td width='19%'align=center><em>Year: </em>$row[4]</td>\n"); print("<td width='19%'align=center><em>Price: </em>$row[6]</td>\n"); print("<td width=10px><font size=1><a href='used_caravan_awnings_edit.php?ID=$row[0]'><font size=1>Delete Record</a></font>"); echo("</table>\n"); } if ($ID == NULL) { //do notning }else{ //get ID $ID=$_GET['ID']; print("ID: $ID"); $query = "DELETE FROM `awnings` WHERE ID LIKE '$ID' "; $result = mysql_query($query); if (mysql_affected_rows() == 1) { print '<p>The stock has been deleted</p>'; }else{ print "<p>Could not delete the entry because <b>" . mysql_error() . "</b>. The query was $query.</p>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/135645-solved-problems-deleting-a-record-from-database/ Share on other sites More sharing options...
rhodesa Posted December 5, 2008 Share Posted December 5, 2008 Step 1: After every mysql_query(), add an or die(mysql_error()): <?php $con = mysql_connect("host", "user", "pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db", $con); $query = "SELECT * FROM `awnings`"; $result = mysql_query($query) or die(mysql_error()); //added it here while ($row = mysql_fetch_row($result)) { echo ("<table border='1' wIDth='100%' align='center'>\n"); echo ("<tr>\n"); print ("<td width='19%' align=center><em>Make: </em>$row[1]</td>\n"); print ("<td width='19%'align=center><em>Model: </em>$row[2]</td>\n"); print ("<td width='19%'align=center><em>Size: </em>$row[3]</td>\n"); print ("<td width='19%'align=center><em>Year: </em>$row[4]</td>\n"); print ("<td width='19%'align=center><em>Price: </em>$row[6]</td>\n"); print ("<td width=10px><font size=1><a href='used_caravan_awnings_edit.php?ID=$row[0]'><font size=1>Delete Record</a></font>"); echo ("</table>\n"); } if ($ID == NULL) { //do notning } else { //get ID $ID = $_GET['ID']; print ("ID: $ID"); $query = "DELETE FROM `awnings` WHERE ID LIKE '$ID' "; $result = mysql_query($query) or die(mysql_error()); //added it here if (mysql_affected_rows() == 1) { print '<p>The stock has been deleted</p>'; } else { print "<p>Could not delete the entry because <b>" . mysql_error() . "</b>. The query was $query.</p>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/135645-solved-problems-deleting-a-record-from-database/#findComment-706698 Share on other sites More sharing options...
Prismatic Posted December 5, 2008 Share Posted December 5, 2008 Since you're checking if the affected rows is 1, why is your query searching for ID's like ID? Just simply delete the ID <?php $con = mysql_connect("host","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db", $con); $query = "SELECT * FROM `awnings`"; $result = mysql_query($query); while($row = mysql_fetch_row($result)) { echo("<table border='1' wIDth='100%' align='center'> <tr> <td width='19%' align=center><em>Make: </em>{$row[1]}</td> <td width='19%'align=center><em>Model: </em>{$row[2]}</td>; <td width='19%'align=center><em>Size: </em>{$row[3]}</td> <td width='19%'align=center><em>Year: </em>{$row[4]}</td> <td width='19%'align=center><em>Price: </em>{$row[6]}</td> <td width=10px><font size=1><a href='used_caravan_awnings_edit.php?ID={$row[0]}'><font size=1>Delete Record</a></font> </table>\n"); } if ($ID == NULL) { //do notning } else { //get ID $ID = $_GET['ID']; print("ID: $ID"); $query = "DELETE FROM `awnings` WHERE ID = '{$ID}'"; $result = mysql_query($query); if(mysql_affected_rows() == 1) { print '<p>The stock has been deleted</p>'; } else { print "<p>Could not delete the entry because <b>" . mysql_error() . "</b>. The query was $query.</p>"; } } ?> Cleaned it up a bit, too. Link to comment https://forums.phpfreaks.com/topic/135645-solved-problems-deleting-a-record-from-database/#findComment-706700 Share on other sites More sharing options...
joshgarrod Posted December 5, 2008 Author Share Posted December 5, 2008 ok i added the or die(mysql_error()); but it hasn't made any difference Link to comment https://forums.phpfreaks.com/topic/135645-solved-problems-deleting-a-record-from-database/#findComment-706702 Share on other sites More sharing options...
joshgarrod Posted December 5, 2008 Author Share Posted December 5, 2008 sorry but its still not working ??? Link to comment https://forums.phpfreaks.com/topic/135645-solved-problems-deleting-a-record-from-database/#findComment-706707 Share on other sites More sharing options...
Prismatic Posted December 5, 2008 Share Posted December 5, 2008 sorry but its still not working ??? if ($ID == NULL) ID is not being set anywhere before in that script, and unless you have register_globals on it never hits the else. Change it to if ($_GET['ID'] == NULL) And try again Link to comment https://forums.phpfreaks.com/topic/135645-solved-problems-deleting-a-record-from-database/#findComment-706710 Share on other sites More sharing options...
joshgarrod Posted December 5, 2008 Author Share Posted December 5, 2008 perfect thanks so much Link to comment https://forums.phpfreaks.com/topic/135645-solved-problems-deleting-a-record-from-database/#findComment-706713 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.