无法从html将数据插入数据库

时间:2017-12-09 08:27:30

标签: php html mysql database

我正在尝试将html文件中的数据插入到我的数据库“customers”

然而,我可以连接到数据库,但无法将html文件中的数据插入到我的数据库中

error的截图     enter image description here 注意:

  1. 我没有为phpmyadmin设置密码,只是将用户名设置为“root”
  2. 所有文件都存储在同一文件夹(C:\ wamp64 \ www)
  3. customers.html客户

    <!DOCTYPE html>
    <html>
    <head>
            <title>customers</title>
    </head>
    <body>
    
      <center><form action="SignUp.php" method="POST">
            <input type="text" name="id" placeholder="customer id"><br>
            <input type="text" name="firstname" placeholder="firstname"><br>
            <input type="text" name="orderno" placeholder="order no"> <br>
            <input type="text" name="city" placeholder="city"><br>
            <input type="text" name="price" placeholder="product price"><br>
            <button type="submit">order</button>
         </form>
    </center>
    </body>
    </html>
    

    SignUp.php

    <?php
    
    include 'db.php';
    
    $sql= "INSERT INTO customers (id,firstname,orderno,city,price)
    VALUES ('345','username','979','city','4465')";
    
     if($result=mysql_query($sql)==true)
            echo "data inserted successfully";
     else
            echo "failed to insert data";
    ?>
    

    db.php中

    <?php
    
    $conn=mysqli_connect("localhost","root","","customers");
    if(!$conn){
            die("connection failed : " .mysqli_connect_error());
    }
    else{
            echo "connection created successfully"."<br>";
    }
    
    ?>
    

1 个答案:

答案 0 :(得分:0)

我不知道这是否是你的问题但是,customers.html你应该让它customers.php,让你的生活更轻松,而且

本声明

<?php

include 'db.php';

$sql= "INSERT INTO customers (id,firstname,orderno,city,price)
VALUES ('345','username','979','city','4465')";

 if($result=mysql_query($sql)==true)
        echo "data inserted successfully";
 else
        echo "failed to insert data";
?>

应该是

<?php

include 'db.php';

$sql= "INSERT INTO customers (id, firstname, orderno, city, price)
VALUES ('345','username','979','city','4465')";

 if($result=mysql_query($sql)==true) {
        echo "data inserted successfully";
 } else {
        echo "failed to insert data";
 }
?>

您的if/else代码中没有括号。

如果您要使用mysqli连接数据库,您可以将SignUp.php转换为

<?php
$sql = "INSERT INTO MyGuests (id, firstname, orderno, city, price)
VALUES ('345', 'username', '979', 'city', '4465')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Also you might wanna take a look at this

As well as this

相关问题