gpt4 book ai didi

php - mySQL 从 PHP 表单添加到数据库时出现语法错误

转载 作者:行者123 更新时间:2023-11-29 07:55:40 25 4
gpt4 key购买 nike

所以我试图允许表单将数据添加到 mySQL 表中。我有这个表格

<form name="addBook" action="addBook.php" method="post" >
ISBN: <input type="text" name="isbn"><br />
Name: <input type="text" name="name"><br />
Edition: <input type="text" name="edition"><br />
Author: <input type="text" name="author"><br />
Class: <input type="text" name="class"><br />
Department: <input type="text" name="department"><br />
Condition: <input type="text" name="condition"><br /><br />
<input type="submit" value="Add Book">
</form>

addBook.php 在哪里...

<?php
$con=mysqli_connect("cclloyd.com","cclloyd","","Inventory");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$isbn = mysqli_real_escape_string($con, $_POST['isbn']);
$name = mysqli_real_escape_string($con, $_POST['name']);
$edition = mysqli_real_escape_string($con, $_POST['edition']);
$author = mysqli_real_escape_string($con, $_POST['author']);
$class = mysqli_real_escape_string($con, $_POST['class']);
$department = mysqli_real_escape_string($con, $_POST['department']);
$condition = mysqli_real_escape_string($con, $_POST['condition']);

$sql="INSERT INTO Books (isbn, name, edition, author, class, department, condition)
VALUES ('$isbn', '$name', '$edition', '$author', '$class', '$department', '$condition')";

if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}

mysqli_close($con);

header('Location: http://umassd.cclloyd.com/bookadded.php' ) ;
?>

当我执行它时,我收到此错误。“错误:您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,了解在“条件”附近使用的正确语法。 VALUES ('l', 'lk', 'l', 'k', 'j', 'h', 'h')' 在第 1 行”

这些只是我在填写表格时随机填写的内容。错误在哪里?网上查了很多,都说像我有的一样输入。

最佳答案

condition是Mysql的保留字。检查保留字here

将单词放在引号中。

关于php - mySQL 从 PHP 表单添加到数据库时出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25227122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com