0

My query below updates a record using variables to identify the data in the DB. I think my syntax is correct although it might be wrong. Also, I am absolutely sure that the variables have legitimate values in them. Why won't this query work?

UPDATE  `databasename`.`".$tablename."` SET  `stock` =  '".$f."' WHERE  `myerspark`.`item_id` ='".$g."' LIMIT 1

Thanks guys. Tom, yes I have tried that and it works fine. But it is frustrating because I echo all three variables at the end of the script and they all display legitimate values.

Hamish, how do I view these errors?

Jon_Darkstar, these variables are assigned in previous lines of code. Here is my entire code block:

//variables $f, $g, and $tablename assigned from POST variables in previous lines
mysql_select_db($database_Yoforia, $Yoforia);
mysql_query("UPDATE `yoforiainventory`.`".$tablename."` SET `stock` =  '".$f."' WHERE `".$tablename."`.`item_id` ='".$g."' LIMIT 1 ");
mysql_close($Yoforia);

echo ($f);
echo ($tablename);
echo ($g);

Again, when i echo these variables, they all come out with good values.

4
  • What error are you getting? Have you tried hard-coding the PHP variable values just to see that it executes fine? Commented Nov 19, 2010 at 0:45
  • Looks fine. What errors do you get from MySQL? They're usually pretty descriptive. Commented Nov 19, 2010 at 0:46
  • if you assign that query to a variable, what's the value of that variable after this line of code? another thing i would do is trying variables inside the string (variable parsing). Commented Nov 19, 2010 at 1:06
  • What are the "good values" that come out? Commented Nov 19, 2010 at 3:47

1 Answer 1

1

I'm kind of confused what belongs to SQL, what belongs to PHP, where that string comes from, etc. What you have might be fine (if there is a double quote in front and end that i dont see.

I'd probably write it like this:

$sql = "UPDATE databasename.$tablename SET stock = '$f' WHERE myerspark.item_id = '$g' LIMIT 1"
$res = mysql_query($sql, $conn).....

you can backtick more stuff (and/or do mysql_real_escape) for 'extra safety;, but that covers the idea.

What is myerspark? i dont see how it relates to the query, that is probably you're real meaningful error, whether there is a syntax error or not. If myerspark is a seperate table from tablename then you've got an issue here, maybe a JOIN you ought to have?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.