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
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.

Link to comment
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.

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.