0

Been trying to figure this out for too long, here is my code

else if(isset($_POST['mytrigger1']))
{
$title1 = ($_POST['titleHOME']);
$title2 = ($_POST['titleLEARN']);
$title3 = ($_POST['titleVTOUR']);
$title4 = ($_POST['titleTRIVIA']);
$title5 = ($_POST['titleALBUMS']);
$title6 = ($_POST['titleFAQS']);

$stmt = $mysqli->prepare("UPDATE  page_title set PAGETITLE = 'title1' where PAGENAME='HOME',PAGETITLE = 'title2' where PAGENAME='LEARN'");
$stmt->execute();

}

I want to update multiple rows with multiple where clauses.

3
  • you can't have multiple WHERE and use AND or OR not commas for WHERE conditions. Commented Jan 20, 2014 at 22:10
  • Out of curiousity, why not do a transaction and do two UPDATE statements? Commented Jan 20, 2014 at 22:10
  • 1
    Also, I don't think your code is correct; you don't seem to be using $title1, $title2, etc. Commented Jan 20, 2014 at 22:12

1 Answer 1

3

At the setout, you should call

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

This enables you don't have to check any return values, just put try { ... } catch { ... } blocks. And do not repeat preparing same statements, reuse it.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

try {

    $mysqli = new mysqli(...);

    if (...) {

       ...

    } else if (isset($_POST['mytrigger1'])) {

        $stmt = $mysqli->prepare("UPDATE page_title set PAGETITLE = ? where PAGENAME = ?");
        foreach ($_POST as $k => $v) {
            if (!is_string($v) || strpos($k, 'title') !== 0) {
                continue;
            }
            $stmt->bind_param('ss', $v, substr($k, 5));
            $stmt->execute();
        }

    }

    ....

} catch (mysqli_sql_exception $e) {

    echo $e->getMessge();

}
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.