表格说它已发送但数据库中没有数据显示

时间:2014-12-04 16:06:48

标签: php html mysql database

我是PHP的新手,仍然试图让我的头脑。这个表单说数据已经发送到数据库,但是当我看到数据库是空的时,没有出现错误?我的代码有问题吗?

注意:我了解此表单不受SQL注入保护。


HTML


<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>Page Form</title>
        <link rel="stylesheet" href="style.css" />
    </head>
    <body>
        <div class="container">
            <div class="main">
                <h2>PHP Page 3 Form</h2><hr/>
                <span id="error">

                </span>
                <form action="page4_insertdata.php" method="post">

                    <label>Company Name :<span>*</span></label><br />
                    <input name="company_name" type="text" placeholder="Joes Cleaner" required>
                    <br />

                    <label>Ref :<span>*</span></label><br />
                    <input name="ref" type="text" placeholder="H123" required>
                    <br />

                    <label>Website :<span>*</span></label><br />
                    <input name="website" type="text" placeholder="www.google.com" required>
                    <br />

                    <label>Email :<span>*</span></label><br />
                    <input name="email" type="email" placeholder="Joescleaners@gmail.com" required>
                    <br />

                    <label>Telephone :<span>*</span></label><br />
                    <input name="tel" type="text" placeholder="07123456789" required>
                    <br />

                    <label>Message :<span>*</span></label><br />
                    <input name="message" id="message" type="text" size="500" required>
                    <br />



                    <input  type="reset" value="Reset" />
                    <input name="submit" type="submit" value="Submit" />

                </form>
            </div>

        </div>
    </body>
</html>

PHP


<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>PHP Multi Page Form</title>
        <link rel="stylesheet" href="style.css" />
    </head>
    <body>
        <div class="container">
            <div class="main">
                <h2>PHP Multi Page Form</h2><hr/>

                <?php

                            $servername = "localhost";
                            $db_database = 'form';
                            $username = "root";
                            $password = "";

                            // Create connection
                            $conn = new mysqli($servername, $username, $password);

                            // Check connection
                            if ($conn->connect_error) {
                                die("Connection failed: " . $conn->connect_error);
                            } 
                            echo "DB Connected successfully. ";


                            $company_name = $_POST['company_name'];
                            $ref = $_POST['ref'];
                            $website = $_POST['website'];
                            $email = $_POST['email'];
                            $tel = $_POST['tel'];
                            $message = $_POST['message'];


                            $sql = "INSERT INTO detail (company_name,ref,website,email,tel,message) 
                            VALUES ('$company_name','$ref','$website','$email','$tel','$message')";

                            if($sql){
                            echo " Database Sent.";
                            }
                            else {
                            echo "ERROR to insert into database";
                            };
                ?>
            </div>

        </div>
    </body>
</html>

2 个答案:

答案 0 :(得分:2)

更改以下代码:

if($sql){
echo " Database Sent.";
}
else {
echo "ERROR to insert into database";
};

要:

$result = $conn->query($sql);
if($result){
    echo " Database Sent.";
}
else {
    echo "ERROR to insert into database";
};

这样您实际上是在执行查询并检查查询失败...

要使您的查询更安全,请尝试以下操作:

$sql = "
    INSERT INTO detail (
        company_name,
        ref,
        website,
        email,
        tel,
        message
    ) 
    VALUES (
        '" . mysqli_real_escape_string($company_name) . "',
        '" . mysqli_real_escape_string($ref) . "',
        '" . mysqli_real_escape_string($website) . "',
        '" . mysqli_real_escape_string($email) . "',
        '" . mysqli_real_escape_string($tel) . "',
        '" . mysqli_real_escape_string($message) . "'
    )";

更好的是,使用以下内容替换$sql实例化和查询执行($conn->query())来使用params的绑定:

$stmt = $conn->prepare("INSERT INTO detail (company_name,ref,website,email,tel,message) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param('ssssss', $company_name, $ref, $website, $email, $tel, $message);
$stmt->execute();

您可以访问PHP: mysqli_stmt::bind_param - Manual

,了解与mysqli的绑定参数

完整代码:

<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>PHP Multi Page Form</title>
        <link rel="stylesheet" href="style.css" />
    </head>
    <body>
        <div class="container">
            <div class="main">
                <h2>PHP Multi Page Form</h2><hr/>

                <?php

                            $servername = "localhost";
                            $db_database = 'form';
                            $username = "root";
                            $password = "";

                            // Create connection
                            $conn = new mysqli($servername, $username, $password, $db_database);

                            // Check connection
                            if ($conn->connect_error) {
                                die("Connection failed: " . $conn->connect_error);
                            } 
                            echo "DB Connected successfully. ";

                            $stmt = $conn->prepare("INSERT INTO detail (company_name,ref,website,email,tel,message) VALUES (?, ?, ?, ?, ?, ?)");
                            $stmt->bind_param('ssssss', 
                                $_REQUEST['company_name'],
                                $_REQUEST['ref'],
                                $_REQUEST['website'],
                                $_REQUEST['email'],
                                $_REQUEST['tel'],
                                $_REQUEST['message']
                            );

                            if($stmt->execute()) {
                                echo " Database Sent.";
                            } else {
                                echo "ERROR to insert into database: " . $stmt->error;
                            };
                ?>
            </div>

        </div>
    </body>
</html>

答案 1 :(得分:0)

您实际上并没有发送查询,而是设置变量$ sql =“INSERT .....” 这总是如此。

你需要这样做:

$result =  $mysqli->query($sql);


 if ($result......)