markerpower Posted June 27, 2007 Share Posted June 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 27, 2007 Share Posted June 27, 2007 You're not quoting the number string. 1,2,3,4 should be "1,2,3,4" or '1,2,3,4'. Wait... are you trying to append to the "numbers" column? If so, try this: UPDATE user_numbers SET numbers = CONCAT(numbers,",2"); Quote Link to comment Share on other sites More sharing options...
markerpower Posted June 28, 2007 Author Share Posted June 28, 2007 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. Quote Link to comment Share on other sites More sharing options...
Illusion Posted June 28, 2007 Share Posted June 28, 2007 try this Update users_table set numbers='$user_id' where user_id='{$userdata['user_id']}'; You should have string data type on numbers columns to do so. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 28, 2007 Share Posted June 28, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.