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>";
}
           }     
?>

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>";
  }
}
?>

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.

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

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.