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

时间:2015-04-11 05:23:42

标签: php mysql forms

当我运行此代码时,它会显示现有数据库中的详细信息,但不会使用放入表单中的条目更新数据库。

<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>

但是,我尝试使用一个简单的echo来代替insert查询,它按照我的预期运行...按下提交按钮之前没有任何内容,然后在输入表单后输入“Nameame:thename。” p>

<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>

我还在另一个处理php文件的表单中测试了替换查询,该文件从一个不同的html页面接收$ POST [],表单上有一个表单,它更新数据库就好了。

<html>

<body>

    <?php

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

        // check connection
        if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n" , 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>

我确信我的问题来自于我必须在一个页面上更新所有内容这一事实,但我对这一切都太过新了,不知道如何处理它。

1 个答案:

答案 0 :(得分:0)

谢谢你下垂,我改变了以下内容:

添加&#39; $ qry-&gt; $ con(sql查询作为方法的属性)&#39;并将该部分移到打印出项目列表的部分上方,以便在用户提交后更新列表。