Hi, i am try to display and insert info on a person into my database but i keep getting this error whenever i enter the info

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 'join (FName, LName) VALUES ('flsdjflsjfl','jsdlfjslfjvlj')' at line 1

<?php
include ("include.php");
if(!$_POST){
	$display_block = "
	<form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">
	<p><strong>First/Last Names:</strong><br/>
	<input type=\"text\" name=\"FName\" size=\"30\" maxlength=\"75\">
	<input type=\"text\" name=\"LName\" size=\"35\" maxlength=\"50\"></p>
	
	<p><strong>Street:</strong><br/>
	<input type=\"text\" name=\"Street\" size=\"50\"></p>
	
	<p><strong>City/State/Zip:</strong><br/>
	<input type=\"text\" name=\"City\" size=\"30\" maxlength=\"50\">
	<input type=\"text\" name=\"State\" size=\"5\" maxlength=\"2\">
	<input type=\"text\" name=\"Zip\" size=\"10\" maxlength=\"10\"></p>
	
	<p><strong>Street Type:</strong><br/>
	<input type=\"radio\" name=\"st_type\" value=\"home\" checked> home
	<input type=\"radio\" name=\"st_type\" value=\"work\">work</p>
	
	<p><strong>Telephone:</strong><br/>
	<input type=\"text\" name=\"Phone\" size=\"30\" maxlength=\"25\">
	<input type=\"radio\" name=\"tel_type\" value=\"home\" checked> home
	<input type=\"radio\" name=\"tel_type\" value=\"work\" checked>work</p>
	
	<p><strong>Email Address:</strong><br/>
	<input type=\"text\" name=\"Email\" size=\"30\" maxlength=\"150\">
	<input type=\"radio\" name=\"email_type\" value=\"home\" checked>home
	<input type=\"radio\" name=\"email_type\" value=\"work\" checked>work</p>
	
	<p><input type=\"submit\" name=\"submit\" value=\"Add Advocate\"></p>
	</form>";
} else if($_POST){
	
	if(($_POST["FName"] == "") || ($_POST["LName"] == "")){
		header("location: addadvocate.php");
		exit;
	}
	
	//connect to database
	doDB();
	
	$add_master_sql = "INSERT INTO join (FName, LName) 
						VALUES ('".$_POST["FName"]."','".$_POST["LName"]."')";
	$add_master_res = mysqli_query($mysqli, $add_master_sql)
					  or die(mysqli_error($mysqli));
					  
	if(($_POST["Street"]) || ($_POST["City"]) || ($_POST["State"])
		|| ($_POST["Zip"])){
		$add_street_sql = "INSERT INTO join (Street, City, State, Zip)
							VALUES ('".$_POST["Street"]."', '".$_POST["City"]."', 
							'".$_POST["State"]."', '".$_POST["Zip"]."')";
		$add_street_res = mysqli_query($mysqli, $add_street_sql)
						   or die(mysqli_error($mysqli));
						   
	}
	
	if($_POST["Phone"]){
		$add_tel_sql = "INSERT INTO join (Phone) VALUES
						('".$_POST["Phone"]."')";
		$add_tel_res = mysqli_query($mysqli,$add_tel_sql)
					   or die(mysqli_error($mysqli));
	}
	
	if($_POST["Email"]){
		$add_email_sql = "INSERT INTO join (Email) VALUES ('".$_POST["Email"]."')";
		$add_email_res = mysqli_query($mysqli, $add_email_sql)
						 or die(mysqli_error($mysqli));
	}
	
	mysqli_close($mysqli);
	$display_block = "<p>Your entry has been added. Thank you! Would you 
	like to <a href=\"addadvocate.php\">add another</a>?</p>" ;
	
}
?>
<html>
<head>
<title>Add an Entry</title>
</head>
<body>
<h1>Add an Entry</h1>
<?php echo $display_block; ?>
</body>
</html>

Dani AI

Generated

The immediate syntax error is caused by using the identifier join as a table name. MySQL treats JOIN as a reserved keyword, so the parser sees the token sequence starting at that word and fails. is correct to suggest renaming the table; is also correct that quoting the identifier is possible, but renaming to something like contacts or advocates is the cleaner, long‑term fix.

There is a second, logical issue in the posted code: multiple INSERT statements into the same table (each one supplying different columns) will create separate rows, not one row with all fields set. For a single person record either INSERT all required columns in one statement, or INSERT the master row and then UPDATE that same row (using the inserted id) for optional fields. Use prepared statements to avoid SQL injection and to make handling NULL/empty values straightforward.

Example (preferred: single prepared INSERT):

$stmt = $mysqli->prepare(
  "INSERT INTO contacts
    (first_name,last_name,street,city,state,zip,street_type,phone,phone_type,email,email_type)
   VALUES (?,?,?,?,?,?,?,?,?,?,?)"
);

$fname = trim($_POST['FName'] ?? '');
$lname = trim($_POST['LName'] ?? '');
$street = trim($_POST['Street'] ?? '');
$city = trim($_POST['City'] ?? '');
$state = trim($_POST['State'] ?? '');
$zip = trim($_POST['Zip'] ?? '');
$streetType = $_POST['st_type'] ?? null;
$phone = trim($_POST['Phone'] ?? '');
$phoneType = $_POST['tel_type'] ?? null;
$email = trim($_POST['Email'] ?? '');
$emailType = $_POST['email_type'] ?? null;

$stmt->bind_param('sssssssssss',
  $fname,$lname,$street,$city,$state,$zip,$streetType,$phone,$phoneType,$email,$emailType
);
$stmt->execute();

Alternate (insert then update using insert_id) is useful if only name is mandatory:

$master = $mysqli->prepare("INSERT INTO contacts (first_name,last_name) VALUES (?,?)");
$master->bind_param('ss',$fname,$lname);
$master->execute();
$id = $mysqli->insert_id;

$upd = $mysqli->prepare("UPDATE contacts SET street=?,city=?,state=?,zip=? WHERE id=?");
$upd->bind_param('ssssi',$street,$city,$state,$zip,$id);
$upd->execute();

Additional troubleshooting notes: check the form handling with $_SERVER['REQUEST_METHOD'] === 'POST' rather than if(!$_POST), use !empty() to test optional fields, remove duplicate checked attributes on radio inputs (only one option per name), enable mysqli error reporting during development, and always prefer parameterized queries over concatenated SQL.

Recommended Answers

All 2 Replies

I think, join is mysql reseverd keyword so rename it and then try again.
Also you must write only one query. Here you are writing so many insert queries for one record (I GUESS). Build single insert query.

Yes urtrivedi is right.
you can always use `(escape) character around table name to avoid it.

.....
INSERT INTO `join`
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.