Hello, I need help with this PHP form using SQL. I am creating a form that post
ID: 657522 • Letter: H
Question
Hello, I need help with this PHP form using SQL. I am creating a form that post data into DATA base but I keep getting this error:
Could not add the entry because:
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 ''form' (ID, firstName, lastName, userName, password, email, date_entered) VALUE' at line 1.
Here is my code:
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Add a User Entry</title>
</head>
<body>
<h1>Add a User Entry</h1>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$dbc = mysql_connect('localhost', 'michaelg', 'Guardian1');
mysql_select_db('michaelg_forms', $dbc);
$problem = FALSE;
if (!empty($_POST['firstName']) && !empty($_POST['lastName']) && !empty($_POST['userName']) && !empty($_POST['password']) && !empty($_POST['email'])) {
$firstName = trim(strip_tags($_POST['firstName']));
$lastName = trim(strip_tags($_POST['lastName']));
$userName = trim(strip_tags($_POST['userName']));
$password = trim(strip_tags($_POST['password']));
$email = trim(strip_tags($_POST['email']));
} else {
print '<p>Please submit entries in the blank boxes below.</p>';
$problem = TRUE;
}
if (!$problem) {
$query = "INSERT INTO 'form' (ID, firstName, lastName, userName, password, email, date_entered) VALUES (0, '$firstName', '$lastName' , '$userName', '$password', '$email', NOW())";
if (@mysql_query($query, $dbc)) {
print '<p>The user entry has been added!</p>';
} else {
print '<p>Could not add the entry because:<br />' . mysql_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
}
}
mysql_close($dbc);
}
?>
<form action="user-form.php" method="post">
<p>First Name: <input type="text" name="firstName" size="40" maxsize="100" /></p>
<p>Last Name: <input type="text" name="lastName" size="40" maxsize="100" /><p>
<p>User Name: <input type="text" name="userName" size="40" maxsize="100" /><p>
<p>Password: <input type="text" name="password" size="40" maxsize="100" /><p>
<p>Email: <input type="text" name="email" size="40" maxsize="100" /><p>
<input type="submit" name="submit" value="Post This Entry!" />
</form>
</body>
</html>
If anyone knows please help. I thought that the problem was because of reserve words but even after I put quotes in the 'form' after INSERT INTO, still did not worked!!
Explanation / Answer
For getting exact error message add a error location in programms,
======
Eg:
To get the error message: $errorMessage=$query_new_user_insert->errorInfo(); print_r($errorMessage);
======
"from" is a keyword in SQL. You may not used it as a column name without quoting it.I would be better you can rename the column and try turning off emulation
======
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.