【问题标题】:unable to insert data into database from html无法从 html 将数据插入数据库
【发布时间】:2017-12-09 08:27:29
【问题描述】:

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

但是,我能够连接到数据库,但无法将 html 文件中的数据插入到我的数据库中

截图error
备注:

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

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>";
}

?>

【问题讨论】:

  • 您将mysqli_* 函数与mysql_ 组合在一起,它们并不相同。
  • 你试过用谷歌搜索消息吗? mysql_* 已弃用,因此不要编写包含它的代码,也不要混合使用 mysql* 和 mysqli*。 “访问被拒绝”看起来使用您输入的凭据无法访问
  • 我没有设置任何凭据密码为空且用户名为“root”

标签: php html mysql database


【解决方案1】:

我不知道这是否是您的问题,但是,您应该改成 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-26
    • 2012-04-13
    • 1970-01-01
    • 2013-03-02
    • 2021-07-23
    相关资源
    最近更新 更多