Jump to content

[SOLVED] Problems deleting a record from database


joshgarrod

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.