如何正确更新我的数据库

时间:2013-04-17 20:25:20

标签: php mysql

我正在尝试使用您从下拉菜单中选择的内容更新我的表格,然后将文本放入文本框中,它将更新特定列(fixture_result)。输出告诉我它已更新但是当我查看数据库时它没有完成它。

我不知道此问题是否曾被提出过。我查看了一些与更新有关的表格,但没有多大帮助;但如果你看到我做错了什么我会很感激。

<html>
<head>
    <title>The Score</title>
</head>
<body>
    <form id="form1" name="form1" method="post">
    Fixture:
        <select name='NEW'>
            <option value="">--- Select ---</option> <br>
            <?php
                if(isset($db_name)&&$db_name!=""){
                    $select=$_POST['NEW'];
                }
            ?>
            <?php
                $list=mysql_query("select * from Fixture_Information order by fixture_id asc");

                while($row_list=mysql_fetch_assoc($list)){
            ?>
            <option value="<?php echo $row_list['fixture_id']; ?>">
                <?php if($row_list['fixture_id']==$select){ echo "selected"; } ?>
                <?php echo $row_list['fixture_name']; ?>
            </option>
            <?php
                }
            ?>
        Input Score: <input type="text" name="myusername">
        <input type="submit" value="Submit">
        <?php
            $data =$_POST['myusername'];
            $select=$_POST['NEW'];
            $sql=("UPDATE Fixture_Information
                set fixture_result = '$data'
                where fixture_name = '$select'");
            $checkresult = mysql_query($sql); 
            if ($checkresult) echo 'update query succeeded'; 
                else echo 'update query failed'; 
            mysql_close(); 
        ?>
        </select>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

您只检查更新语句的成功,在您的情况下为true。但是,您不检查更新的行数。您可以将mysql_affected_rows用于此

if (mysql_affected_rows() > 0)
    echo 'update query succeeded';
else
    echo 'update query failed';
相关问题