![MySQL 插入表-语法问题](https://linux22.com/image/1499003/MySQL%20%E6%8F%92%E5%85%A5%E8%A1%A8-%E8%AF%AD%E6%B3%95%E9%97%AE%E9%A2%98.png)
$servername = 'edited';
$username = 'edited';
$password = 'edited';
$dbname = 'edited';
$name = $_POST ['name'];
$email = $_POST ['email'];
$number = $_POST ['number'];
$manufacturer = $_POST ['manufacturer'];
$birth = $_POST ['birth'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = mysqli_query($conn, "INSERT INTO hgcasestudy (name, email, number, phonename, birthday) VALUES ($name, $email, $number, $manufacturer, $birth)");
if (!$sql) {
die('Invalid query: ' . mysql_error());
}
$conn->close();
我不断收到错误消息:连接成功无效查询:拒绝用户“edited”@“localhost”访问(使用密码:否)
我很困惑,因为 $servername 没有设置为本地?我认为这与页面的其他部分语法不一致。有什么想法吗?非常感谢。
答案1
您已将 MySQL 与 MySQLi 混合使用,这绝对行不通。您使用 MySQLi 进行连接,然后尝试使用 MySQL 进行查询。您的查询应该是:
$sql = mysqli_query($conn,"INSERT INTO hgcasestudy (name, email, number, phonename, birthday) VALUES ($name, $email, $number, $manufacturer, $birth)");
还应该指出的是,如果使用未经任何检查或清理的表单值,则很容易受到注入攻击。