0

My current echo $sql; returns two queries:

UPDATE 00001 SET discount=11, discount_price=166.43, discount_price=166.00 WHERE code=00001070170UPDATE 00001 SET discount=11, discount_price=166.43, discount_price=166.00 WHERE code=00001070171

I was wondering how I can separate them and add a ; at the end of each one. My $sql command follows:

$sql = ("UPDATE ".$row['sku']." SET discount=".$discount1.", discount_price=".$discount_price1.", discount_price_elite=".$discount_price." WHERE code=".$row['code']."");

And the exected output is:

UPDATE 00001 SET discount=11, discount_price=166.43, discount_price=166.00 WHERE code=00001070170;
UPDATE 00001 SET discount=11, discount_price=166.43, discount_price=166.00 WHERE code=00001070171
10
  • The mysqli plugin for PHP does not allow branched/combined statements. It only allows one statement at a time execution. You'll have to use parameterized prepared statements and just execute them with different bound paraemters. Commented Sep 17, 2018 at 17:12
  • Any code ideas @clearshot66 ? Commented Sep 17, 2018 at 17:14
  • Google prepared statements in a loop, the top 5 all explain how to do this Commented Sep 17, 2018 at 17:18
  • for example stackoverflow.com/questions/19106963/… Commented Sep 17, 2018 at 17:19
  • @clearshot66 Technically your first statement is incorrect. Mysqli can execute combined statements, if combined correctly; but it is not secure. Commented Sep 17, 2018 at 17:26

1 Answer 1

1

Don't try to combine them into a single query, do them as separate queries:

foreach ($rows as $row) {
    $sql = "UPDATE ".$row['sku']." SET discount=".$discount1.", discount_price=".$discount_price1.", discount_price_elite=".$discount_price." WHERE code=".$row['code'];
    mysqli_query($mysqli, $sql);
}

BTW, it's a very strange database design to have a separate table for each SKU. There should normally be just a single products table, with an sku column.

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

Comments

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.