Page 1 of 1

mysql syntax question

Posted: Tue Feb 14, 2006 10:12 am
by ssand
I have the following mysql statement:

Code: Select all

$sql = "UPDATE inv_type SET inv_type_name='$type_name', inv_type_function='$type_function'
						WHERE inv_type_name=$original_name";
Which appeared to be working correctly, as it wouldn't create an error. But when veiwing the database it didn't actually change anything. Eventhough I was displaying all values directly before the UPDATE... so the variables were correct.

But when I change the WHERE to read:

Code: Select all

$sql = "UPDATE inv_type SET inv_type_name='$type_name', inv_type_function='$type_function'
						WHERE inv_type_name='$original_name' ";
With single quotes around the $original_name it works correctly.

I assume that I should have the single quotes around all variables?

Thanks

EDIT: The reason I ask is because I copied the first $sql statement from a different page changed some values and the original statement didn't use single quotes around the last variable. So, I'm wondering why one works without the quotes and one doesn't.

Posted: Tue Feb 14, 2006 10:22 am
by feyd
that would depend on what is sent via $original_name.. if it's not a number you will have a syntax error.

Posted: Tue Feb 14, 2006 10:28 am
by ssand
Ah, that would explain it then.
The copied code was using a number in the value. This new variable doesn't.

Thanks.

Re: mysql syntax question

Posted: Tue Feb 14, 2006 10:42 am
by Benjamin

Code: Select all

$sql = "UPDATE `inv_type` SET `inv_type_name`='$type_name', `inv_type_function`='$type_function'
						WHERE `inv_type_name`=$original_name";
It's also good practice to place backticks ` around table and field names as it can cause a query to fail in certain circumstances without them.