0

I'm pretty new to PHP and mySQL, and so I'm really confused as to why this isn't working:

$row = mysql_fetch_array($result);

$response["id"] = $row["id"];
mysql_query(" DELETE FROM balloons WHERE ID='$response['id']'");
$response["name"] = $row["name"];
$response["message"] = $row["message"];
$response["city"] = $row["city"];
$response["time"] = $row["date"];

$response["success"] = 1;

I basically want to delete the row right away. I've tried a ton of different things, and I think my syntax must be wrong somewhere. I tried saying

$todelete = $response["id"];
mysql_query(" DELETE FROM balloons WHERE ID='$todelete'");

But no good. What's odd is if I say

 mysql_query(" DELETE FROM balloons WHERE ID=4");

it deletes the row with the ID of 4 just fine.

I'm sure this is a really dumb mistake, can someone educate me?

2

7 Answers 7

1

check this

mysql_query(" DELETE FROM balloons WHERE ID='$todelete'");

change this line to

mysql_query(" DELETE FROM balloons WHERE ID='".$todelete."'");
0
1

This looks like a string formatting issue. Here are some options:

mysql_query("DELETE FROM balloons WHERE ID = '{$row['id']}'");

mysql_query("DELETE FROM balloons WHERE ID = '" . $row['id'] . "'");

// Or try this

$todelete = $row["id"];

mysql_query("DELETE FROM balloons WHERE ID = '$todelete'");

mysql_query("DELETE FROM balloons WHERE ID = '" . $todelete . "'");

Make sure your code is not susceptible to SQL Injection!

6
  • None of these worked :( I think it must be due to the type of $response['id']. For instance, saying $id = 7 and then doing a DELETE WHERE id=$id works just fine. But saying $id = $response['id'] is no good.
    – bob
    Jun 26, 2013 at 6:03
  • @user1903064 - You placed the string within mysql_query()?
    – Aiias
    Jun 26, 2013 at 6:04
  • @user1903064 - Updated answer to use $row['id'].
    – Aiias
    Jun 26, 2013 at 6:09
  • I'm at a loss. I don't want to waste any more of your time, since this is such an odd issue that perhaps something else is going wrong beyond my knowledge... but I fail to understand why $id = 8, "DELETE FROM balloons WHERE id=$id" works, whereas $id=$row['id'] "DELETE FROM balloons WHERE id=$id" doesn't work. I've even tried casting $id as an int... This is so odd. EDIT and I know $row['id'] isn't null, since I'm echoing it and it's always a number. So confused.
    – bob
    Jun 26, 2013 at 6:14
  • 1
    Thanks. I will continue looking into this and update when/if I find the answer!
    – bob
    Jun 26, 2013 at 6:18
0

Store the value in a variable first

 $id=$response['id'];
 mysql_query("DELETE FROM balloons WHERE ID='$id'");

if 'id' is an integer not need to keep in quotes. you can write like this

 mysql_query("DELETE FROM balloons WHERE ID=$id");
1
  • I've actually tried this, no good. I honestly have no clue why. I'm looking at the other suggestions now... (but thanks!)
    – bob
    Jun 26, 2013 at 5:56
0

Try this one:

$todelete = $row["id"];

$query = "DELETE FROM balloons WHERE ID='$todelete'";

0
0

Are you getting the correct value for $row['id'] and $result ?

Have you tried dumping $result and $row['id']?

Try

 var_dump($result);

If it gives you the correct array with the values you want. You can do:

 $row = mysql_fetch_array($result);

 $todelete = $row["id"];
 mysql_query(" DELETE FROM balloons WHERE ID='$todelete'");

Or You might need to do this :

 while($row = mysql_fetch_array()){
           $todelete = $row["id"];
           mysql_query(" DELETE FROM balloons WHERE ID='$todelete'");


  }

Try doing var_dump() of $result, $row, $row['id'] etc on each steps to DEBUG.

0

All you need to do is this-

mysql_query(" DELETE FROM balloons WHERE ID=' " .$response['id']. " ' ");

or rather just use this

mysql_query(" DELETE FROM balloons WHERE ID=' " .$row['id']. " ' ");

Also, I hope you checked if you are are getting the correct result into row['id']

0

Try this.

mysql_query("DELETE FROM default_ps_products WHERE ID='".$todelete."'");

or you can use this also

mysql_query("DELETE FROM default_ps_products WHERE ID=$todelete");

it will work fine. hope you found your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.