Teaching myself SQL and PHP at the same time and am getting this error:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name, Last Name, Course_id, Professor) VALUES ('duke','john','doe','cpsc','teacher' at line 1 with query
My code is supposed to take the information from the forms and insert it into a database that I already creadI've been looking at it for hours and have searched for the internet for a while, but still can't find whats causing this. Can anyone help me out?
Here's my test.php file with the forms:
<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>
and here's my handler.php file that stores the information from the forms into the database:
<!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>
Thanks