Jump to content

Recommended Posts

I have a variable $type which is a string.  I am trying to update a db table where an element matches that string and increment it by 1.  I am using the following format.

 

mysql_query("UPDATE tablename set '$type'='$type'+1 where id = '$playerid'");

 

This format is not working and I suspect the culprit is the set '$type'='$type'+1.  Can anyone help me on how it should be formatted?

Link to comment
https://forums.phpfreaks.com/topic/133916-solved-formatting-question/
Share on other sites

It's not clear what you want

 

Maybe this:

 

mysql_query("UPDATE tablename set 'type'='type'+1 where id = '$playerid'");

 

Or maybe this:

 

mysql_query("UPDATE tablename set 'type'=$type+1 where id = '$playerid'");

 

 

Does $type hold a column name?

 

Good question. Didn't think of that

You need to use backticks (`) around column names and single quotes(') around data.

 

mysql_query("UPDATE tablename set `$type`= ($type+1) where id = '$playerid'");

 

Also does $type hold a column name? (oh I see thats already been pointed out oh well.)

If there's column name in $type, then updating the column to its name +1 has little sense...

 

mysql_query("UPDATE tablename set `$type`=`$type`+1 where id = '$playerid'");

 

Let me give an example

 

$type="cname";

 

I want the equivalent of

 

mysql_query("UPDATE tablename set cname=cname+1 where id = '$playerid'");

 

but using the $type variable.

$type_data = $type . "1";

mysql_query("UPDATE tablename set `$type`= '$type_data' where id = '$playerid'");

 

or

 

mysql_query("UPDATE tablename set `$type`= '" . $type . "1' where id = '$playerid'");

 

Should do the trick.

 

What I am trying to do is update column $type with the value contained in that column + 1

 

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.