同时自学 SQL 和 PHP 并收到此错误:
错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在 'Name, Last Name, Course_id, Professor) VALUES ('duke','john','doe','cpsc','teacher' at line 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>
谢谢