dqxz96998 2015-04-11 05:23
浏览 17

无法在一个php页面上更新db

When I run this code, it displays the details from the existing DB but does not update the DB with the entries that are put into the form.

<html>
<body>
    <center>
        <h2>Insert Product Details</h2>
        <hr />
    </center>

    <?php
        include('db_conn.php'); //db connection

        if($result = $con->query("SELECT * FROM dbName")){
            if($result->num_rows){
                $rows = $result->num_rows;

                if(isset($_POST['updated'])){ 
                    $qry = "INSERT INTO dbName (ID, Name, Price, Description)
                    VALUES (trim('$_POST[inID]'), trim('$_POST[inName]'), trim('$_POST[inPrice]'), trim('$_POST[inDescription]') )";
                }    

                echo '<table>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Price</th>              
                        <th>Description</th>
                    </tr>
                ';

                    while($row = $result->fetch_object()) {
                                    echo '<tr>', 
                                        '<td>', $row->ID, '</td>', 
                                        '<td>', $row->Name, '</td>', 
                                        '<td>', $row->Price, '</td>', 
                                        '<td>', $row->Description, '</td> </tr>
                                    ';
                    }

                echo '
                    <form method="POST" >
                        </table>
                            <hr />

                            <table>
                                <tr>
                                    <td>ID:</td>
                                    <td>    
                                            <input type = "text"
                                            name = "inID"
                                            value = " "
                                            size = "3">
                                    </td>
                                </tr>
                                <tr>
                                    <td>Name:</td>
                                    <td>    
                                            <input type = "text"
                                            name = "inName"
                                            value = " "
                                            size = "30">
                                    </td>
                                </tr>
                                <tr>
                                    <td>Price:</td>
                                    <td>    
                                            <input type = "text"
                                            name = "inPrice"
                                            value = " "
                                            size = "7">
                                    </td>
                                </tr>
                                <tr>
                                    <td>Description:</td>
                                    <td>    
                                            <input type = "text"
                                            name = "inDescription"
                                            value = " "
                                            size = "60">
                                    </td>
                                </tr>
                            </table>

                            <input type = "submit" value = "insert" name="updated">
                    </form>
                ';

            }
        }       


        /* close connection */
        $con->close();
    ?>
</body>

However, I have tried a simple echo in place of the insert query and it runs as I would expect...nothing before the submit button is pressed and then "Name entered: thename once the form has been submitted.

<html>
<body>
    <center>
        <h2>Insert Product Details</h2>
        <hr />
    </center>

    <?php
        include('db_conn.php'); //db connection

        if($result = $con->query("SELECT * FROM dbName")){
            if($result->num_rows){
                $rows = $result->num_rows;

                if(isset($_POST['updated'])){ 
                    echo "Name entered: " , $_POST[inName];
                }    

                echo '<table>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Price</th>              
                        <th>Description</th>
                    </tr>
                ';

                    while($row = $result->fetch_object()) {
                                    echo '<tr>', 
                                        '<td>', $row->ID, '</td>', 
                                        '<td>', $row->Name, '</td>', 
                                        '<td>', $row->Price, '</td>', 
                                        '<td>', $row->Description, '</td> </tr>
                                    ';
                    }

                echo '
                    <form method="POST" >
                        </table>
                            <hr />

                            <table>
                                <tr>
                                    <td>ID:</td>
                                    <td>    
                                            <input type = "text"
                                            name = "inID"
                                            value = " "
                                            size = "3">
                                    </td>
                                </tr>
                                <tr>
                                    <td>Name:</td>
                                    <td>    
                                            <input type = "text"
                                            name = "inName"
                                            value = " "
                                            size = "30">
                                    </td>
                                </tr>
                                <tr>
                                    <td>Price:</td>
                                    <td>    
                                            <input type = "text"
                                            name = "inPrice"
                                            value = " "
                                            size = "7">
                                    </td>
                                </tr>
                                <tr>
                                    <td>Description:</td>
                                    <td>    
                                            <input type = "text"
                                            name = "inDescription"
                                            value = " "
                                            size = "60">
                                    </td>
                                </tr>
                            </table>

                            <input type = "submit" value = "insert" name="updated">
                    </form>
                ';

            }
        }       


        /* close connection */
        $con->close();
    ?>
</body>

I have also tested the replaced query in another form handling php file that receives the $POST[]'s from a different html page with a form on it and it updates the database just fine.

<html>

<body>

    <?php

        $con = new mysqli("localhost", "me", "mypassword","dbName");

        // check connection
        if (mysqli_connect_errno()) {
        printf("Connect failed: %s
" , mysqli_connect_error());
        exit();
        }

        // insert values        
        $qry = "INSERT INTO dbName (ID, Name, Price, Description)
        VALUES (trim('$_POST[inID]'), trim('$_POST[inName]'), trim('$_POST[inPrice]'), trim('$_POST[inDescription]') )";

        // print out
        if ($con->query($qry) === TRUE) {
            echo "It Worked"
        } else {
            echo "Error: " . $qry . "<br>" . $con->error;
        }

        // close connection
        $con->close();


    ?>

</body>

I am sure my problem comes from the fact that I am having to update all on one page but I am way too new at all of this to know what to do about it.

  • 写回答

1条回答 默认 最新

  • dshdb64088 2015-04-11 06:16
    关注

    Thank you to droopy, I have changed the following:

    Added '$qry->$con(sql query as attribute of the method)' and move that section above the section where it prints out the list of items so that the list updates after a user submits.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大