表格不会更新,但显示成功

时间:2014-10-17 20:13:05

标签: php mysql database

我是MYSQL的新手

当我尝试更新数据库时,结果显示成功,但该项目未显示。请有人帮忙。通过将帖子变成会话变量,我尝试将其结束,但没有任何区别。

update_ac.php

                $name = $_POST['name'];
            $lastname = $_POST['review'];

            // Connect to server and select database.
            mysql_connect("$host", "$username", "$password")or die("cannot connect");
            mysql_select_db("$db_name")or die("cannot select DB");

            // update data in mysql database
            $sql="UPDATE reviews SET name='$name', review='$lastname' WHERE id='$id'";
            $result=mysql_query($sql);

            // if successfully updated.
            if($result){
            echo "Successful";
            echo "<BR>";
            echo "<a href='list_reviews.php'>View result</a>";
            }

            else {
            echo "ERROR";
            echo "Welcome ". $_POST['name']. "<br />";
            echo $lastname; 
            }

            ?> 

update.php

                // Connect to server and select database.
            mysql_connect("$host", "$username", "$password")or die("cannot connect");
            mysql_select_db("$db_name")or die("cannot select DB");

            // get value of id that sent from address bar
            $id=$_GET['id'];

            // Retrieve data from database
            $sql="SELECT * FROM $tbl_name WHERE id='$id'";
            $result=mysql_query($sql);

            $rows=mysql_fetch_array($result);
        ?>

        <?php include "includes/header.php"; ?>
                <div id="bodywrap">
                    <div id="leftcol">
                    <h1>Update the form</h1>

        <form name="form1" method="post" action="update_ac.php">
        <label>Name</label>
        <input name="name" type="text" id="name" value="<? echo $rows['name']; ?>">
        <br />
        <label>Review</label>
        <input name="lastname" type="text" id="lastname" value="<? echo $rows['review']; ?>" size="15">
        <br />
        <input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
        <br />
        <input type="submit" name="Submit" value="Submit">
        </form>

        <?php
        // close connection
        mysql_close();
        ?>

1 个答案:

答案 0 :(得分:1)

$name = $_POST['name'];
$lastname = $_POST['review'];

您是否注意到您使用了$ id而不是$ _POST ['id']或$ _REQUEST ['id']或$ _GET ['id']?错过了这个:

$id = $_POST['id'];

SQL工作正常,因为它在找到的每个id =''上都有DID更新,所以这不是错误。

另外,正如其他人所指出的那样,使用mysqli,mysql扩展很危险,可能很快就会被弃用