我正在尝试将html文件中的数据插入到我的数据库“customers”
中然而,我可以连接到数据库,但无法将html文件中的数据插入到我的数据库中
error的截图 注意:
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>";
}
?>
答案 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();
?>