0

So I have this

$sql_totalbooknumber =  "SELECT SUM(items_counter) FROM probid_categories WHERE items_counter>0 AND `category_id <>355";
$sql_updatebooknumber = "UPDATE `probid_categories` SET `items_counter` = ".$sql_totalbooknumber." WHERE  `category_id` =  '355'";


            if (!mysql_query($sql_totalbooknumber))
  {
  die('Error: ' . mysql_error());
  };
            if (!mysql_query($sql_updatebooknumber))
  {
  die('Error: ' . mysql_error());
  };

somehow they work fine individually (if i comment out one or the other) but when I combine them I get this error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT SUM(items_counter) FROM probid_categories WHERE items_counter>0 AND `cate' at line 1

1
  • 1
    AND `category_id remove the ` Commented Oct 4, 2012 at 18:01

2 Answers 2

2

$sql_totalbooknumber is a string with your query in it. Not the results of that query.

You're expecting an integer to be passed in to $sql_updatebooknumber, but you're passing the string that contains your first query into it.

Try this:

$sql_totalbooknumber =  "SELECT SUM(items_counter) FROM probid_categories WHERE items_counter>0 AND `category_id` <>355";
$result = mysql_query($sql_totalbooknumber);
list($id) = mysql_fetch_array($result);

$sql_updatebooknumber = "UPDATE `probid_categories` SET `items_counter` = ".$id." WHERE  `category_id` =  '355'";
mysql_query($sql_updatebooknumber);
Sign up to request clarification or add additional context in comments.

4 Comments

@andrewsi has a single-pass solution to this that is also less...SQL-injectionny.
He's not passing any user input in, so there's no risk of SQL Injection. Unless the 355 is user input.
Oh gotcha, I didn't realize he was trying to do a subquery.
Any data should be escaped. You start to assume and you eventually make serious mistakes.
2

You're doing a subquery; you really need to wrap it in brackets:

$sql_totalbooknumber =  "SELECT SUM(items_counter) FROM probid_categories WHERE items_counter>0 AND `category_id` <>355";
$sql_updatebooknumber = "UPDATE `probid_categories` SET `items_counter` = (".$sql_totalbooknumber.") WHERE  `category_id` =  '355'";

Otherwise, the database can't parse it properly.

6 Comments

You're missing a backtick by category_id
i get You can't specify target table 'probid_categories' for update in FROM clause
@AdamPlocher - Well-spotted. Thank you! (Though I did take that from the question - I wonder if that might also not be helping with the code not working?)
i added the back-tick already, i think this applies xaprb.com/blog/2006/06/23/… for this error because i had this yesterday
I'm at the limit of my knowledge of subqueries - have you tried the answer from @AdamPlocher ?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.