dongqiao3927 2014-10-17 20:13
浏览 15

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

I am new to MYSQL

When i try to update a database, the result is showing successful, but the item is not showing. Please can someone help. I have tried carrying it over, by turning the post into a session variable, but made no difference.

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条回答 默认 最新

  • douhuxi4145 2014-10-17 20:59
    关注
    $name = $_POST['name'];
    $lastname = $_POST['review'];
    

    Did you notice you used $id and not $_POST['id'] or $_REQUEST['id'] or $_GET['id']? Missing this:

    $id = $_POST['id'];
    

    The SQL worked because it DID update on every id='' it found, so it was not an error.

    Also, as others pointed out, use mysqli, mysql extension is dangerous and probably will be deprecated soon

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题