简体   繁体   中英

My html form is not posting values into the database using php mysql

I'm currently developing a soccer league and would like to update my games table, I have given each game a game_id and want to update the scores using it as a condition in my SQL. my games table has game_id, home_team_id, home_score,away_score, away_team_id, date, location....

My SQL query:

<?php require_once("includes/functions.php");?>
<?php 
    if(isset($_POST['update'])){
        $errors = array();

    //form validation
    $required_fields = array("game_id", "home_score", "away_score", );
    foreach($required_fields as $fieldname) {
    if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) &&
        $_POST[$fieldname] != 0)) { $errors[] = $fieldname; }
    }

    if (empty($errors)) {
    // Perform Update


    $id = mysql_prep($_POST['game_id']);
    $home_score = mysql_prep($_POST['home_score']);
    $away_score = mysql_prep($_POST['away_score']);


    $query = "UPDATE games SET 
    home_score = {$home_score}, 
    away_score = {$away_score} 
    WHERE games.game_id= {$id}";
    $result = mysql_query($query, $connection);
    if (mysql_affected_rows() == 1) {
    // Success
    $message = "The scorers were successfully updated";
    } else {
    // Failed
    $message = "The scorers update failed ";
    $message .= "<br />" . mysql_error();
    }

    } else {
    // Errors occurred
    $message = "There were " . count($errors) . " errors in the form.";
    }

    } // end: if (isset($_POST['submit']))

?>



 <?php include("includes/header.php");?>
<div class="document">
    <div class="navigation">
    <br />
    <div class="content">
        <h2>Edit Fixture</h2>
        <form action="edit_fixture.php" method="post">

        <p>Game Id:
            <select type="int" name="game_id" >
            <?php
                for($count=1; $count <= 70; $count++) {
                    echo "<option value=\"{$count}\">{$count}</option>";
                }
            ?>
            </select>
        </p>
        <p>Home Score: 
            <select type="text" name="home_score" >
            <?php
                for($count=0; $count <= 9; $count++) {
                    echo "<option value=\"{$count}\">{$count}</option>";
                }
            ?>
            </select>
        </p>
        <p>Away Score: 
            <select type="text" name="away_score" >
            <?php
                for($count=0; $count <= 9; $count++) {
                    echo "<option value=\"{$count}\">{$count}</option>";
                }
            ?>
            </select>
        </p>

        <input type="submit" value="Update" />
        </form>
        <br />
        <a href="content.php">Cancel</a>
    </div>
    </div>
<?php require("includes/footer.php");?>     
if(isset($_POST['update'])){

the post variable you're trying to get is different than the submit button

<input type="submit" value="Update" />

Update != update so the sql functions aren't taking place because the post variable isn't set

$_POST['update'] is always null so your post doesn't get called.

Add name="update" to your submit button

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