Help with UPDATE

Hey guys. I’m trying to update my database with new information from a form on another page.

mysql_query(“UPDATE software SET pcname=’$new_value’ WHERE pcname=’$edit_machine’”);

^This works great and I have no trouble with it. But what I want to do is use to a variable to decide what table gets updated like below

mysql_query(“UPDATE software SET ‘$software_item’=’$new_value’ WHERE ‘$software_item’=’$edit_machine’”);

But ^this doesn’t work for me. The script runs without fatal errors but nothing actually happens. Is it possible to do this with variables for the field?

Thanks in advance.

Ok. Maybe I fixed this by myself…but I’m not really sure why/how. Must be divine intervention.

mysql_query(“UPDATE software SET $software_item=’$new_value’ WHERE pcname=’$edit_machine’”);

^This seems to work for exactly what I need it to do. But I’m not sure why $software_item did not need the ’ ’ like the others seem to. . .

It has to do with SQL and formatting. It is usually …

field = ‘value’,

Values almost always need the single quotes. Fields do not.

If you think about it, when your query is parsed and sent, it would like this to MySQL. Which is incorrect syntax:

UPDATE software SET ‘myfield’ = ‘myvalue’ WHERE ‘myfield2’ = ‘myField1’

Hope That Helps.

Thanks Ragster. That makes perfect sense.

Sponsor our Newsletter | Privacy Policy | Terms of Service