user2579395
user2579395

Reputation: 77

Update multiple rows in PDO - 1 Column change

Im trying Update multiple rows in PDO. My table scripts contains columns id, style and link, as follows:

id  style       link

1   Normalize   http://cdnjs.cloudflare.com/ajax/libs/normalize/2.1.0/normalize.css
2   Bootstap    http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.css

I need save two different links for Normalize and Bootstrap, but when I use this construction, interpreter return something wrong. How Can I do this? I want change first link, or second or both together or each separately.

My sql:

$connect = new Database();
$connect->query("UPDATE scripts SET link :normalize WHERE style = 'Normalize' AND  SET link :bootstrap WHERE style = 'Bootstrap'");
$connect->bind(':normalize', $_POST['normalize']);
$connect->bind(':bootstrap', $_POST['bootstrap']);
$connect->execute();

Upvotes: 1

Views: 514

Answers (2)

Mihai
Mihai

Reputation: 26784

Try this:

UPDATE scripts SET link = CASE
WHEN style= 'Normalize' THEN :normalize
WHEN style= 'Bootstrap' THEN :bootstrap
ELSE link
END;

Upvotes: 1

TheWolf
TheWolf

Reputation: 1385

I don't think what you try to do is possible. You will need multiple queries if you want to UPDATE rows using multiple conditions.

Upvotes: 0

Related Questions