Jump to content


Photo

Something wrong with this query


  • Please log in to reply
3 replies to this topic

#1 gnuffo1

gnuffo1
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 04 February 2006 - 04:53 PM

I have the variable stocklevel, which when echoed displays the correct amount, but after I do this query:

$updatestock = "UPDATE products SET products_quantity='$stocklevel' WHERE  products_id=$products_id";
$result = mysql_query($updatestock) or die ('Your stock query doesn't work: ' . mysql_error());

The php page it's in appears to load fine, doesn't even have any errors, but the field in the DB is ALWAYS updated to 0, no matter what the $stocklevel contains, even though it echos a completely different value on the page. I did have the same query before, but without the ' marks around $stocklevel and that displayed the error saying there was incorrect syntax near '=73' (if 73 was $stocklevel)

What I especially don't get is that just above that query in the php code, I have this one for updating the price, which works absoloutley fine and is in EXACTLY the same format:

$updateprice = "UPDATE products SET products_price=$price WHERE products_id=$products_id";
$result = mysql_query($updateprice) or die ('Your price query doesn't work: ' . mysql_error());

This one works absoloutley fine all the time, even when I was getting an error messege for the stock one when I didn't have the ' marks around $stocklevel, and as you can see is in exactly the same format as the stock one. I don't understand it at all.

Another weird thing is the stock update query was working fine until I stuck the "or die" bit on the end. Ironically, maybe the error message is causing the problem?
[a href="http://www.sentinelpc.co.uk" target="_blank"]Sentinel Technology - Custom PCs and Laptops[/a]

#2 gnuffo1

gnuffo1
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 04 February 2006 - 05:06 PM

No, just got rid of the die bit off the end of the the stock update and it still doesn't work. I don't get any errors displayed and the page appears to work, displays the right amount (this is without the ' around $stocklevel) but when I check the database it just doesn't want to update. I've even put curly braces around each query, so:

{
$updateprice = "UPDATE products SET products_price=$price WHERE products_id=$products_id";
$result = mysql_query($updateprice) or die ('Your price query doesn't work: ' . mysql_error());
}
{
$updatestock = "UPDATE products SET products_quantity=$stocklevel WHERE products_id=$products_id";
$result = mysql_query($updatestock);
}

but to no avial. And just in case you noticed the ' in doesn't for the error messege - that's not what's really in the error messege in the code, I actually have some swear words in it because I'm so annoyed and forgot that you can't have ' unless you \ them when I was censoring, but I digress.
[a href="http://www.sentinelpc.co.uk" target="_blank"]Sentinel Technology - Custom PCs and Laptops[/a]

#3 gnuffo1

gnuffo1
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 04 February 2006 - 05:47 PM

I've fixed it. The problem was that $stocklevel actually contained some html code in it as well, since I scanned it from an external website, but I put the substr bit to remove that code in the wrong if statement and hence it was trying to insert text into a int field; the error was displaying the text in the browser, but because it was mainly html tags the browser didn't output them to the screen so I didn't notice.
[a href="http://www.sentinelpc.co.uk" target="_blank"]Sentinel Technology - Custom PCs and Laptops[/a]

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 04 February 2006 - 07:40 PM

Glad you got it working -- for future reference, I tend to echo the query string itself in PRE tags just make sure there's nothing funny going on.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users