-2

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>
crablab
  • 25
  • 1
  • 3
  • 9

1 Answers1

1

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">
Phil
  • 157,677
  • 23
  • 242
  • 245
  • Is it not possible to continue using it this way? All my other queries are formatted thus and I would prefer not go around fixing them. – crablab Jan 28 '15 at 22:27
  • 1
    Of course it's possible but it's hardly a wise decision – Phil Jan 28 '15 at 22:28
  • I see. I did some debugging and it appears the form isn't posting...even though it was a few minutes ago. Running the query with a harcoded variable for forname edited the table as expected, – crablab Jan 28 '15 at 22:29
  • Having full error reporting would have alerted you to the fact that `$_POST['forname']` didn't exist – Phil Jan 28 '15 at 22:31
  • Yeah, I take the point and on future projects I will read up on this. – crablab Jan 28 '15 at 22:34