Jump to content

[SOLVED] formatting question


gamesmstr

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

 

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.