简体   繁体   中英

Update with a prepared statement and bind row with an id

My database consist of the following columns: id, openingHours, records .

I have a HTML table where I get printed out rows from the MySQL database. To the right of each HTML row I have a button where I can click Select. When I click Select I need to update the column records with +1 with the belonging column id .

That query is working in phpMyAdmin if I use the query like this:

sqli = ("UPDATE stores SET records = records + 1 WHERE id = 333);

But I need to bind the id on the row where I use the select button. When I click the select button with this code, the row in phpMyAdmin is not getting updated. What am I doing wrong? I get the error: Undefined index: id in /Applications/MAMP/htdocs/storeproject/updaterecords.php on line 8 Updated Succesfull.

HTML

<form action="updaterecords.php" method="post">
   <button type="submit" class="btn btn-success" name="submit">Select</button>
   <?php include 'updaterecords.php' ?>
</form>

updaterecords.php

<?php error_reporting(E_ALL); ini_set('display_errors', 1);

if (mysqli_connect_errno()) { echo "Error: no connexion allowed : " . mysqli_connect_error($mysqli); } 
?>
<?php

if(isset($_POST['submit'])) {
$id = $_POST['id']; //line 8

    $stmt = $mysqli->prepare("UPDATE stores SET records = records + 1 WHERE id =".$id);

    $stmt->bind_param('i', $id);   

    if ($stmt->execute()) { 
        $success = true;
    }

    $stmt->close();

    $mysqli->close();   
    if($success) {
        echo "Updated Succesfull";
    } else {
        echo "Failed: " .  $stmt->error;
      }
    }

?>

进行这些更改

$stmt = $mysqli->prepare("UPDATE stores SET records = records + 1 WHERE id= ?");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM