Jump to content

PHP/MySQL Help With Commas


markerpower

Recommended Posts

Hello,

 

I need some help. I'm basically trying to figure out how to add multiple numbers seperated by commas in one column. Such as "1,2,3,4".

 

Each time my script is ran it's suppose to add a new number in the column after the old number.

 

My code is basically like this:

 

$user_id = $user_id . "," . ($user_id + 1);

 

$sql = "UPDATE " . USERS_TABLE . " SET numbers = " . ($user_id) . " WHERE user_id = " . $userdata['user_id'];

 

 

But I get an error like:

SQL Error : 101 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2 WHERE user_id = 2' at line 1

UPDATE user_numbers SET numbers= ,2 WHERE user_id = 2

 

It seems like it is having problems adding the 2 after the comma, and I can't figure out why. If anyone can help I appreciate it.

Link to comment
Share on other sites

I'm not sure how to add CONCAT into the current format of the following:

$sql = "UPDATE " . USERS_TABLE . " SET numbers = " . ($user_id) . " WHERE user_id = " . $userdata['user_id'];

 

Maybe to make it clearer of what my script is suppose to do.

 

Lets say numbers = 1. When my script runs the first time, numbers= 1,2. When it runs another time, numbers= 1,2,3. This is how the numbers should appear in the database. With the numbers, and the numbers seperated by commas.

 

 

Currently when my script runs it replaces the old number with the new number. For example if numbers= 1, then when the script runs the first time,  numbers= 2. That's not what I want. I want the 1 to stay with the addition of the comma and 2.

Link to comment
Share on other sites

I'm not sure how to add CONCAT into the current format of the following:

$sql = "UPDATE " . USERS_TABLE . " SET numbers = " . ($user_id) . " WHERE user_id = " . $userdata['user_id'];

 

Maybe to make it clearer of what my script is suppose to do.

 

Lets say numbers = 1. When my script runs the first time, numbers= 1,2. When it runs another time, numbers= 1,2,3. This is how the numbers should appear in the database. With the numbers, and the numbers seperated by commas.

 

 

Currently when my script runs it replaces the old number with the new number. For example if numbers= 1, then when the script runs the first time,  numbers= 2. That's not what I want. I want the 1 to stay with the addition of the comma and 2.

 

Okay, so my second suggestion applies.  To incorporate it, just assemble a query string in PHP to resemble the one with CONCAT in it.  And as Illusion said, your "numbers" column must be a VARCHAR or TEXT column.

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.