MySQL SQL数据未出现在数据库中

使用此脚本,将在PHP文档中返回值,说明订单中包含的内容,并且每次在表单上按下“提交”按钮时,数据库都会创建一个新行(和ID),但是所引用列的每一行中的数据为空白(表示连接有效,并且某些数据已从表单中拉出,但PHP变量未提取)。我想将PHP变量发送到mysql数据库,并使原始数据出现在表中。

请帮忙,下面两套代码:

<html>

    <head>

        <title> Auto parts ordering</title>

    </head>

    <body>

        <form action="processorder.php" method="post">

            <table border="0">

                <tr>
                    <td> First Name</td>
                    <td align="centre"><input type="text" name="fname" size="30" /></td>
                </tr>

                <tr>
                    <td> Last Name</td>
                    <td align="centre"><input type="text" name="lname" size="30" /></td>
                </tr>

                <tr>
                    <td> Email Address</td>
                    <td width="15"align="centre"><input type="text" name="email" size="30" /></td>
                </tr>

            </table>

            <table border="0">

                <tr bgcolor=#cccccc>
                    <td width="15"> Item:</td>
                    <td width="15"> Qty:</td>
                </tr>

                <tr>
                    <td> Tyres</td>
                    <td align="centre"><input type="text" name="tyreqty" size="3" maxlength="3" /></td>
                </tr>

                <tr>
                    <td> Oil</td>
                    <td align="centre"><input type="text" name="oilqty" size="3" maxlength="3" /></td>
                </tr>

                <tr>
                    <td> Spark Plugs</td>
                    <td align="centre"><input type="text" name="sparkqty" size="3" maxlength="3" /></td>
                </tr>

                <tr>
                    <td colspan="3" align="centre"><input type="submit" value="Submit Order" /></td>
                </tr>

            </table>

        </form>

    </body>

</html>
<?php

    $servername = "localhost";
    $username = "root";
    $password = "REDACTED";
    $dbname = "Orders";

    $con = mysqli_connect($servername, $username, $password, $dbname);

    if($con === false){
        die("ERROR: Could not connect. " . mysqli_connect_error());
    }

    $sql = "INSERT INTO active (id, tyre, oil, fname, lname, email) VALUES (NULL, '".$tyreqty."', '".$oilqty."', '".$fname."', '".$lname."', '".$email."')";
    !mysqli_query($con, $sql);


    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $email = $_POST['email'];
    $tyreqty = $_POST['tyreqty'];
    $oilqty = $_POST['oilqty'];
    $sparkqty = $_POST['sparkqty'];
?>

<html>

    <head>

        <title> Auto parts ordering</title>




    </head>

    <body>

        <h1>Wills Auto Parts </h1>
        <h2> Order Results </h2>



        <?php
            echo 'Thank You '.$fname. '!';
            echo "<p> Order processed at " .date('h:i, j F Y')."</p>";

            echo "<h2> Your order contains: </h1>";
            echo $tyreqty.' Tyres <br />';
            echo $oilqty.' Bottles of Oil <br />';
            echo $sparkqty.' Spark Plugs <br />';

            echo "Confirmation Email sent to you at: " .$email;


        ?>


    </body>

</html>
评论
  • 洛可可
    洛可可 回复

    在准备SQL语句之前,您需要分配POST值。

    试试这个代码。希望它能解决您的问题。

    <?php
    
        $servername = "localhost";
        $username = "root";
        $password = "REDACTED";
        $dbname = "Orders";
    
        $con = mysqli_connect($servername, $username, $password, $dbname);
    
        if($con === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());
        }
    
        $fname = $_POST['fname'];
        $lname = $_POST['lname'];
        $email = $_POST['email'];
        $tyreqty = $_POST['tyreqty'];
        $oilqty = $_POST['oilqty'];
        $sparkqty = $_POST['sparkqty'];
    
        $sql = "INSERT INTO active (id, tyre, oil, fname, lname, email) VALUES (NULL, '".$tyreqty."', '".$oilqty."', '".$fname."', '".$lname."', '".$email."')";
    
        if (mysqli_query($con, $sql)) {
           echo "New record inserted successfully";
        } else {
           echo "Error: " . $sql . "<br>" . mysqli_error($con);
        }
    
    mysqli_close($conn);
    ?>
    
    <html>
    
        <head>
    
            <title> Auto parts ordering</title>
    
    
    
    
        </head>
    
        <body>
    
            <h1>Wills Auto Parts </h1>
            <h2> Order Results </h2>
    
    
    
            <?php
                echo 'Thank You '.$fname. '!';
                echo "<p> Order processed at " .date('h:i, j F Y')."</p>";
    
                echo "<h2> Your order contains: </h1>";
                echo $tyreqty.' Tyres <br />';
                echo $oilqty.' Bottles of Oil <br />';
                echo $sparkqty.' Spark Plugs <br />';
    
                echo "Confirmation Email sent to you at: " .$email;
    
    
            ?>
    
    
        </body>
    
    </html>