PHP mySqli not updating table

2019-03-07 02:15发布

问题:

I am trying to update the table but nothing is changing. The database name and fields are correct.

<?php
require("config.php");
    $forname  = $_POST['name'];
    $newval = "yes";
    mysqli_query($con, "UPDATE pupils SET signin = '$newval' WHERE forname = '$forname'");
    mysqli_close($con); 
?>

Help appreciated! Thanks,

UPDATE

Appears that data is not posting correctly for some reason.

<form class="form-inline" name="markin" role="form" method="POST" action="markin.php">
    <div class="form-group">
    <select class="form-control" name"name" id="name">
    <?php
    $query = "SELECT * FROM `pupils` WHERE signin = 'no'";//Grab the data
    $result = mysqli_query($con, $query);
    while ($row = mysqli_fetch_array($result)) {//Creates a loop to loop through results
    echo "<option>" . $row['forname'] . "</option>";//$row['index'] the index here is a field name
    }
    ?>
    </select>
    </div>
    <button type="submit" class="btn btn-success">Mark in</button>
    </form>

回答1:

This is a terrible way to use the mysqli extension.

First, I suggest you read the prepared statements quickstart guide which would lead you to something like this...

$stmt = $con->prepare('UPDATE pupils SET signin = ? WHERE forname = ?');
$stmt->bind_param('ss', $newval, $forname);
$stmt->execute();

As an added bonus, you should set mysqli to throw exceptions on error so you don't have to check return values all the time. In your config.php file, before creating the connection, do this...

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$con = new mysqli(...); // or however you create $con

and as mentioned in the comments, the php.ini file in your development environment should enable full error reporting with the following directives

error_reporting = E_ALL
display_errors = On

To directly answer your question, you're missing an equals sign for the <select> elements name attribute. It should be

<select class="form-control" name="name" id="name">


标签: php mysqli