Jump to content

Delete 1 row from array of datat (php/mysql)


shortysbest

Recommended Posts

I'm trying to delete just 1 row from my table of information however when i click delete on the row i am trying to delete it just deletes all of the rows in the entire table. I have been able to delete it defining the ID i want to delete manually however it HAS to be done with a click of a button for whichever row so my client can delete from it whenever he wants.

 

basically the table is setup like this:

| id | storename | name | location | website |

-----------------------------------------------------------------

| 1 | google | John Doe | USA | www.google.com | Delete |

| 2 | facebook | Jason M. | Africa | www.facebook.com | Delete |

 

So when i click delete next to row 1, i want just that row to be removed. Or if i click Delete next to Row 2 i want just row 2 to be deleted.

Heres my code:

 

Code executed when you click delete button:

 

if (isset($_POST['drow'])){
	$drows = $row['id'];
mysql_query("DELETE FROM dealers WHERE id='$drows' LIMIT 1");
print '<h3>'.$storename.' has been deleted</h3>';
}

 

Code for showing all of the table data

 

<?php 
$query1 = mysql_query("SELECT * FROM dealers ORDER BY storename ");
print '<p><h1><b>Note: </b> Click on a row to view dealer\'s website <em>(if available)</em></h1>';
Print '<div class="table">';
Print '<div class="tableheader"><div class="idrow">#</div><div class="row">Store Name</div><div class="row">Owner\'s Name</div><div class="row">Location</div></div>';
$i=0+1;
while ($row = mysql_fetch_array($query1))
{
$drow = $row['id'];
$storename = $row['storename'];
$name = $row['name'];
$location = $row['location'];
$website = $row['website'];
if ($row['website']!==""){
$lwebsite = 'target="_blank" href="'.$row['website'].'"';
}
else {
$lwebsite = '';
}

if (isset($_POST['drow'])){
	$drows = $row['id'];
mysql_query("DELETE FROM dealers WHERE id='$drows' LIMIT 1");
print '<h3>'.$storename.' has been deleted</h3>';
}

print '<a '.$lwebsite.'><div class="rows"><div class="idrow">'.$i++.' '.$row['id'].'';
print '<form action="index.php?node=dealers" method="POST"><input class="button" name="drow" type="submit" value="X"></form>';
print '</div><div  class="row">'.$storename.'</div><div class="row">'.$name.'</div><div class="row">'.$location.'</div></div></a>';
}
Print '</div>';
?>

 

 

Thanks in advanced.. Im sure this is rather Easy to do just i cannot figure out the correct block of code.

First, separate out your DELETE query from the mysql_query(), and then echo it to make sure it contains the string you would expect it to contain.

 

if (isset($_POST['drow'])){
	$drows = $row['id'];
        $query = "DELETE FROM dealers WHERE id=$drows LIMIT 1";
        echo "DELETE query: " . $query . "<br />;
mysql_query($query);
print '<h3>'.$storename.' has been deleted</h3>'; //This will echo regardless of whether the query succeeds or fails . . .
}

First, separate out your DELETE query from the mysql_query(), and then echo it to make sure it contains the string you would expect it to contain.

 

if (isset($_POST['drow'])){
	$drows = $row['id'];
        $query = "DELETE FROM dealers WHERE id=$drows LIMIT 1";
        echo "DELETE query: " . $query . "<br />;
mysql_query($query);
print '<h3>'.$storename.' has been deleted</h3>'; //This will echo regardless of whether the query succeeds or fails . . .
}

 

so using this it still deletes all rows :| It does echo out the correct information however, just it doesn't only delete the row where i click delete.

OK, it looks like the problem here is going to be that you have this block of code within the while loop. Therefore, if $_POST['drow'] is set, it will loop through every record and delete it. You need to separate the DELETE logic from the display logic.

   if (isset($_POST['drow'])){
      $drows = $row['id'];
      mysql_query("DELETE FROM dealers WHERE id='$drows' LIMIT 1");
      print '<h3>'.$storename.' has been deleted</h3>';
   }

OK, it looks like the problem here is going to be that you have this block of code within the while loop. Therefore, if $_POST['drow'] is set, it will loop through every record and delete it. You need to separate the DELETE logic from the display logic.

   if (isset($_POST['drow'])){
      $drows = $row['id'];
      mysql_query("DELETE FROM dealers WHERE id='$drows' LIMIT 1");
      print '<h3>'.$storename.' has been deleted</h3>';
   }

 

 

Well, now that partially fixes the problem. Now it just deletes one row.. Thanks for that. However the problem occurring now is that it removes the top row each time i click delete to one of the rows.

OK, it looks like the problem here is going to be that you have this block of code within the while loop. Therefore, if $_POST['drow'] is set, it will loop through every record and delete it. You need to separate the DELETE logic from the display logic.

   if (isset($_POST['drow'])){
      $drows = $row['id'];
      mysql_query("DELETE FROM dealers WHERE id='$drows' LIMIT 1");
      print '<h3>'.$storename.' has been deleted</h3>';
   }

 

 

Ahhh Nevermindd.... There's the problem. The query i was refering my row['id'] to was selecting it from the table, but also ordering it by a different variable to display in the table itself, so i just simply had to create a new query just to fetch the id from the rows and use that and it works perfect. Thanks alot for that simple solution lol.. Knew it was something(s) silly easy :) 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.