-3

Need to update different values with different WHERE.

$facebook = $_POST["facebook"];
$twitter = $_POST["twitter"];
$instagram = $_POST["instagram"];

$sql = "UPDATE footer SET link='$facebook' WHERE name='Facebook' AND SET link='$twitter' WHERE name='Twitter' AND SET link='$instagram' WHERE name='Instagram'";
6
  • 1
    Can you provide a little more detail? Commented Mar 7, 2016 at 11:15
  • $sql = "UPDATE footer SET link='$facebook' WHERE name='Facebook' AND SET link='$twitter' WHERE name='Twitter' AND SET link='$instagram' WHERE name='Instagram'"; is not working Commented Mar 7, 2016 at 11:19
  • I need to update facebook,twitter and instagram at once Commented Mar 7, 2016 at 11:19
  • 2
    Why don't do 3 queries? otherwise try something like this: stackoverflow.com/a/20255203/5368495 Commented Mar 7, 2016 at 11:19
  • Thanks everyone for helping :) Commented Mar 7, 2016 at 11:45

1 Answer 1

0

You can do this with case expression:

$sql = "UPDATE footer SET link = case when name='Facebook' then '$facebook'
                                      when name='Twitter' then '$twitter' 
                                      when name='Instagram' then '$instagram'
                                  end
Sign up to request clarification or add additional context in comments.

4 Comments

Working perfectly! Thank you.
This would have been a better answer if the errors that the OP made in their syntax were explained. As in UPDATE only uses one SET and one WHERE clause. dev.mysql.com/doc/refman/5.7/en/update.html
@JohnDoe You're welcome John.
@Fred-ii- You're right, was in a hurry:) usually I'm posting an explanation

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.