0

I am trying to update varchar cell in SQL users table. Now the value of groups_id is 3. $last_id = 4. I want to change it to 3, 4. Could you please tell me what I am doing wrong?

With this code the value remains the same

$sql = "UPDATE registration.users SET groups_id = groups_id+', $last_id' WHERE username = '$user_name'";
$update_groups_id = $db->query($sql);

2 Answers 2

1
$val = $groups_id . ", ".$last_id;
$sql = "UPDATE registration.users SET `groups_id` = '$val' WHERE username = '$user_name'";
$update_groups_id = $db->query($sql);

your SQL query is wrong, you are not concatenating variables properly, try doing this way, I think it should help you

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Do i need to use SELECT in order to get $groups_id?
yes, @KibbleShop, first select $groups_id otherwise it will show undefined variable error
1

There is a syntax fault in your $sql object as you use +', $last_id'. If you want to append in PHP you can use . in string context

Also I'm pretty sure you can leave the '' from the variables so '$last_id' will become $last_id

But more important is that you do not check for any security issues. I hope $user_name and $last_id are not just taken from the input as SQL injections are possible.

I recommend you to look at mysqli_prepare and mysqli_bind

2 Comments

Thanks. I will add checking. For your recomendation I tried groups_id = groups_id+''.$last_id it still shows and error. Is this what you mean?
@KibbleShop I assumed you used + as an operator to append a new string. If that is what you want you have to use . -> "I have a variable a ". $a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.