繁体   English   中英

我的HTML表单未使用php mysql将值发布到数据库中

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

我目前正在开发一个足球联赛,并且想更新我的游戏表,我给每个游戏都提供了一个game_id,并希望使用它作为SQL中的条件来更新得分。 我的游戏表中有game_id,home_team_id,home_score,away_score,away_team_id,日期,位置...。

我的SQL查询:

<?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'])){

您尝试获取的post变量与Submit按钮不同

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

更新!=更新,因为未设置post变量,所以不会发生sql函数

$_POST['update']始终为null因此不会调用您的帖子。

name="update"添加到您的提交按钮

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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