将日期插入MySQL数据库

将日期插入MySQL数据库,mysql,sql,sql-insert,Mysql,Sql,Sql Insert,我正在编写一个脚本,将有关书籍的数据插入数据库 $errors=array(); foreach(array('title','author','publisher','pub_date','isbn','Format','genre','category','bookcase','shelf','user_id') as $key=>$val){ $_REQUEST[$key] = mysqli_real_escape_string($ptah,trim($_REQUEST

我正在编写一个脚本,将有关书籍的数据插入数据库

$errors=array();

 foreach(array('title','author','publisher','pub_date','isbn','Format','genre','category','bookcase','shelf','user_id') as $key=>$val){

    $_REQUEST[$key] = mysqli_real_escape_string($ptah,trim($_REQUEST[$val])) ;

};
    $title = $_REQUEST['title'] ; $title = strip_tags($title);

    $author = $_REQUEST['author'] ; $author = strip_tags($author);

    $publisher = $_REQUEST['publisher'] ; $publisher = strip_tags($publisher);

    $pub_date = $_REQUEST['pub_date'] ; $pub_date = strip_tags($pub_date);

    $isbn = $_REQUEST['isbn'] ; $isbn = strip_tags($isbn);

    $format = $_REQUEST['Format'] ; $format = strip_tags($format);

    $genre = $_REQUEST['genre'] ; $genre = strip_tags($genre);

    $category = $_REQUEST['category'] ; $category = strip_tags($category);

    $bookcase = $_REQUEST['bookcase'] ; $bookcase = strip_tags($bookcase);

    $shelf = $_REQUEST['shelf'] ; $shelf = strip_tags($shelf);

    $username = $_REQUEST['user_id'] ; $username = strip_tags($username);

# On success, register user
if (empty($errors))

# Insert the user into the database
{
    $insert_sql = "INSERT INTO library (title, author, publisher, pub_date, isbn, format, genre, category, bookcase, shelf, time_entered, by) VALUES ( '$title', '$author', '$publisher', '$pub_date', '$isbn', '$format', '$genre', '$category', '$bookcase', '$shelf', NOW(), '$username' )";
mysqli_query($ptah,$insert_sql) or die(mysqli_error($ptah));

mysqli_close($ptah);
exit();
};
?>
这是插入数据的代码
$errors=array();

 foreach(array('title','author','publisher','pub_date','isbn','Format','genre','category','bookcase','shelf','user_id') as $key=>$val){

    $_REQUEST[$key] = mysqli_real_escape_string($ptah,trim($_REQUEST[$val])) ;

};
    $title = $_REQUEST['title'] ; $title = strip_tags($title);

    $author = $_REQUEST['author'] ; $author = strip_tags($author);

    $publisher = $_REQUEST['publisher'] ; $publisher = strip_tags($publisher);

    $pub_date = $_REQUEST['pub_date'] ; $pub_date = strip_tags($pub_date);

    $isbn = $_REQUEST['isbn'] ; $isbn = strip_tags($isbn);

    $format = $_REQUEST['Format'] ; $format = strip_tags($format);

    $genre = $_REQUEST['genre'] ; $genre = strip_tags($genre);

    $category = $_REQUEST['category'] ; $category = strip_tags($category);

    $bookcase = $_REQUEST['bookcase'] ; $bookcase = strip_tags($bookcase);

    $shelf = $_REQUEST['shelf'] ; $shelf = strip_tags($shelf);

    $username = $_REQUEST['user_id'] ; $username = strip_tags($username);

# On success, register user
if (empty($errors))

# Insert the user into the database
{
    $insert_sql = "INSERT INTO library (title, author, publisher, pub_date, isbn, format, genre, category, bookcase, shelf, time_entered, by) VALUES ( '$title', '$author', '$publisher', '$pub_date', '$isbn', '$format', '$genre', '$category', '$bookcase', '$shelf', NOW(), '$username' )";
mysqli_query($ptah,$insert_sql) or die(mysqli_error($ptah));

mysqli_close($ptah);
exit();
};
?>
提交时,我得到以下错误

您的SQL语法有错误;查看与MySQL服务器版本对应的手册,以了解使用接近“by”值的正确语法(“轻轻地做”、“Hunter Alan”、“Robinson”、“2010”、“1234567890”位于第1行

这漏掉了格式、流派、类别、书架、输入的日期以及完整的输入者

有趣的是,提交的数据量将随单个片段的长度而变化

$errors=array();

 foreach(array('title','author','publisher','pub_date','isbn','Format','genre','category','bookcase','shelf','user_id') as $key=>$val){

    $_REQUEST[$key] = mysqli_real_escape_string($ptah,trim($_REQUEST[$val])) ;

};
    $title = $_REQUEST['title'] ; $title = strip_tags($title);

    $author = $_REQUEST['author'] ; $author = strip_tags($author);

    $publisher = $_REQUEST['publisher'] ; $publisher = strip_tags($publisher);

    $pub_date = $_REQUEST['pub_date'] ; $pub_date = strip_tags($pub_date);

    $isbn = $_REQUEST['isbn'] ; $isbn = strip_tags($isbn);

    $format = $_REQUEST['Format'] ; $format = strip_tags($format);

    $genre = $_REQUEST['genre'] ; $genre = strip_tags($genre);

    $category = $_REQUEST['category'] ; $category = strip_tags($category);

    $bookcase = $_REQUEST['bookcase'] ; $bookcase = strip_tags($bookcase);

    $shelf = $_REQUEST['shelf'] ; $shelf = strip_tags($shelf);

    $username = $_REQUEST['user_id'] ; $username = strip_tags($username);

# On success, register user
if (empty($errors))

# Insert the user into the database
{
    $insert_sql = "INSERT INTO library (title, author, publisher, pub_date, isbn, format, genre, category, bookcase, shelf, time_entered, by) VALUES ( '$title', '$author', '$publisher', '$pub_date', '$isbn', '$format', '$genre', '$category', '$bookcase', '$shelf', NOW(), '$username' )";
mysqli_query($ptah,$insert_sql) or die(mysqli_error($ptah));

mysqli_close($ptah);
exit();
};
?>
比如说

您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以了解使用接近“by”值的正确语法(第1行的“爬出窗口并消失的百岁老人”) 甚至连标题都没写完

您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以了解使用接近“by”值('a'、'b'、'c'、'1234'、'1'、'平装本'、'小说'、'幻想'、'a1''的正确语法 一直到书柜


我被难住了。谁能帮忙吗。

BY
是MySQL中的一个保留字,所以你应该用backticks
`
对它进行转义,以防你需要将它用作字段名

<...> , time_entered, `by`) <...>
,输入的时间,`by`)
作者是一位。要将其用作查询中的标识符,需要使用反勾号将其括起来:

... time_entered, `by`) VALUES (...

通常最好的做法是始终使用反勾号将标识符(列名、表名等)括起来。它对查询引擎更为明确。

当遇到语法错误时,应将注意力集中在文本“near”之后的左侧。在您的情况下,正如答案中所述,错误在于关键字“by”;文本的其余部分(加起来总是80个字符)只是为了让您更容易地找到语法错误所在的位置。