Jump to content

Php variables and MySql


Michdd

Recommended Posts

I'm using this as an UPDATE function:

 

$result = mysql_query("UPDATE monsters SET $updatewhat='$updateto' WHERE $updatewhat='$where'") 
or die(mysql_error());  

 

But I want to make that  $updatewhat='$where' also be WHERE $updatewhat='$updatemon'

 

So in theory it would be:

 

$result = mysql_query("UPDATE monsters SET $updatewhat='$updateto' WHERE $updatewhat='$where' WHERE $updatewhat='$updatemon'") 
or die(mysql_error());  

 

However, that's incorrect syntax.

Link to comment
https://forums.phpfreaks.com/topic/129150-php-variables-and-mysql/
Share on other sites

The reason why, is because your setting to to a variable. so say we used $updatewhat as paper and $updateto as ink and $where as blood.

 

This is what your statement would do:

$result = mysql_query("UPDATE monsters SET $updatewhat="ink" WHERE $updatewhat="blood" ")

 

You never told SQL to define the variables.

Try this (Note i did not test this.)

 

$result = mysql_query("UPDATE monsters SET '$updatewhat' = '$updateto' WHERE '$updatewhat' = '$where'")
or die(mysql_error());

However, I still think the SET $updatewhat should be defined and not a variable. True you might have to add in more code, but it would be less confusing and easier to change.

The reason why, is because your setting to to a variable. so say we used $updatewhat as paper and $updateto as ink and $where as blood.

 

This is what your statement would do:

$result = mysql_query("UPDATE monsters SET $updatewhat="ink" WHERE $updatewhat="blood" ")

 

You never told SQL to define the variables.

Try this (Note i did not test this.)

 

$result = mysql_query("UPDATE monsters SET '$updatewhat' = '$updateto' WHERE '$updatewhat' = '$where'")
or die(mysql_error());

However, I still think the SET $updatewhat should be defined and not a variable. True you might have to add in more code, but it would be less confusing and easier to change.

 

You see, the problem is that it will then change all the fields that are $where. I want it to only update the one field in the 1 row that is shown as $updatemon

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.