gpt4 book ai didi

MySQL 错误 : Inserting form data into Database

转载 作者:行者123 更新时间:2023-11-29 03:06:04 26 4
gpt4 key购买 nike

同时自学 SQL 和 PHP,但出现此错误:

错误:您的 SQL 语法有误;查看与您的 MySQL 服务器版本相对应的手册,了解在“姓名、姓氏、类(class) ID、教授”附近使用的正确语法1 带查询

我的代码应该从表单中获取信息并将其插入到我已经知道的数据库中我已经查看了数小时并在互联网上搜索了一段时间,但仍然找不到导致的原因这。谁能帮帮我?

这是我的 test.php 文件,格式如下:

<html>
<head>
<title>Insert data into database</title>
</head>

<body>
<form method="post" action="handler.php">
<p>What is your University:</p>
<input type="text" name="uni"><br>

<p>What is your First Name:</p>
<input type="text" name="f_name"><br>

<p>What is your Last Name:</p>
<input type="text" name="l_name"><br>

<p>What is your Course:</p>
<input type="text" name="cou_id"><br>
<p>Who was your Professor for that course?:</p>

<input type="text" name="pro_id"><br>
<input type="submit" value="Submit" />
</form>
</body>
</html>

这是我的 handler.php 文件,它将表单中的信息存储到数据库中:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$uni = $_POST['uni'];
$uni= mysql_real_escape_string($uni);
$f_name = $_POST['f_name'];
$f_name = mysql_real_escape_string($f_name);
$l_name = $_POST['l_name'];
$l_name = mysql_real_escape_string($l_name);
$cou_id = $_POST['cou_id'];
$cou_id = mysql_real_escape_string($cou_id);
$pro_id = $_POST['pro_id'];
$pro_id = mysql_real_escape_string($pro_id);

$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("tester") or die(mysql_error());

if (!mysql_query("INSERT INTO my_hw (University, First Name, Last Name, Course_id, Professor) VALUES ('$uni','$f_name','$l_name','$cou_id','$pro_id')"))
{
die('Error: ' . mysql_error() . " with query ". $sql);
}
echo "1 record added";

mysql_close($con);
?>
</body>
</html>

谢谢

最佳答案

您的名字和姓氏列中有空格:

 INSERT INTO my_hw (University, `First Name`, `Last Name`, Course_id, Professor) VALUES     ('$uni','$f_name','$l_name','$cou_id','$pro_id')

注意添加的反引号。

关于MySQL 错误 : Inserting form data into Database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14998468/

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