I'm new to PHP and databases and when running the following code I keep getting the 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 'Table (input1
) VALUES ('test2')' at line 1." The program is intended to get a string from a form and store it in a database.
This is the HTML file:
<html>
<head>
<title>Php Website</title>
</head>
<body>
<form action="index.php" method="POST">
<p> Input 1: <input type="text" name="input1"> </p>
<input type="submit" value="Submit">
</form>
</body>
</html>
This is the PHP file:
define('db_name', 'DataTest');
define('db_user', 'root');
define('db_password', '');
define('db_host', 'localhost');
$link = mysql_connect(db_host, db_user, db_password);
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(db_name, $link);
if (!$db_selected)
{
die('Cannot use ' . db_name . ': ' . mysql_error());
}
$value = $_POST['input1'];
$sql = "INSERT INTO Table (`input1`) VALUES ('$value')";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
mysql_close();
?>
<html>
<head>
<title>Php Website</title>
</head>
<body>
</body>
</html>