Page 1 of 1

Error code 1064:

Posted: Tue Dec 13, 2011 10:20 am
by krobinson937
Unable to execute the query

Error code 1064: 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 'SQLstring' at line 1

Code: Select all

<!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>Register</title>
<link href="php_styles.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1>Registration</h1>
<?php
if (empty($_GET['first_name']) || empty($_GET['last_name']) ||
empty($_GET['phone']) || empty($_GET['address']) ||
empty($_GET['city']) || empty($_GET['state']) ||
empty($_GET['zip']) || empty($_GET['email']))
	exit("<p>You must enter values in all fields of the New Diver Registration form! Click your browswer's Back button to return to the previous page.</p>");
	
$DBConnect = @mysqli_connect("localhost", "test", "test")
	Or die("<p>Unable to connect to the database server.</p>"
	. "<p>Error code " . mysqli_connect_errno()
	. ": " . mysqli_connect_error()) . "</p>";
	
$DBName = "scuba_school";
if (!@mysqli_select_db($DBConnect, $DBName)) {
	$SQLstring = "CREATE DATABASE $DBName";
	$QueryResult = @mysqli_query($DBConnect, SQLstring)
		Or die("<p>Unable to execute the query</p>"
		. "<p>Error code " . mysqli_errno($DBConnect)
		. ": " . mysqli_error($DBConnect)) . "</p>";
	echo "<p>Successfully created the database.</p>";
	mysqli_select_db($DBConnect, $DBName);
	
$TableName = "divers";
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = @mysqli_query($DBConnect, $SQLstring);

if (!$QueryResult) {
	$SQLstring = "CREATE TABLE divers (diverID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY, First VARCHAR(40), Last VARCHAR(40),
	Phone VARCHAR(40), Address VARCHAR(40), City VARCHAR(40), State VARCHAR(2), Zip VARCHAR(10))";
	$QueryResult = @mysqli_query($DBConnect, $SQLstring)
		Or die("<p>Unable to create the divers table.</p>"
		. "<p>Error code " . mysqli_errno($DBConnect)
		. ": " . mysqli_error($DBConnect)) . "</p>";
	echo "<p>Successfully created the divers table.</p>";
	}
}

	$First = addslashes($_GET['first_name']);
	$Last = addslashes($_GET['last_name']);
	$Phone = addslashes($_GET['phone']);
	$Address = addslashes($_GET['address']);
	$City = addslashes($_GET['city']);
	$State = addslashes($_GET['state']);
	$Zip = addslashes($_GET['zip']);
	$Email = addslashes($_GET['email']);
	
$SQLstring = "INSERT INTO divers VALUES(NULL, '$First', '$Last', '$Phone', '$Address', '$City', '$State', '$Zip')";
$QueryResult = @mysqli_query($DBConnect, $SQLstring)
Or die("<p>Unable to execute the query.</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "</p>";

$DiverID = mysqli_insert_id($DBConnect);		
	mysqli_close($DBConnect);
?>

<p>Thanks <?= $First ?>! Your new diver ID is <strong><?= $DiverID ?></strong>.</p>
<form action="CourseListings.php" method="get">
<p><input type="submit" value="Register for Classes" />
<input type="hidden" name="diverID" value="<?= $DiverID ?>" /></p>
</form>
</body>
</html>

Re: Error code 1064:

Posted: Tue Dec 13, 2011 1:15 pm
by califdon
You are missing the $ in front of "SQLString" in your first query.

Re: Error code 1064:

Posted: Tue Dec 13, 2011 1:59 pm
by mikosiko
@krobinson937: Do you have any reason to include your CREATE DATABASE and CREATE TABLE in this script?... normally those kind of sentence (DDL's or DCL's) are executed just one single time, and also normally using other kind of tools, like PHPMYAdmin or similar (even when you can do it with a specific PHP script for only that purpose too)... mixing DDL's/DCL's with DML's is not really a good practice.

DDL's= Data Definition Language
DCL's= Data Control Language
DML's= Data Manipulation Language

in another note... you should also eliminate the error control operator (@) from your code... you must control the possible errors no suppress them, otherwise your script could die without even inform/log why

Re: Error code 1064:

Posted: Tue Dec 13, 2011 2:12 pm
by Amanda1998
@califdon makes good point (You are missing the $ in front of "SQLString" in your first query.).. add to that I wanna said that, First I will try to use

Code: Select all

if ($_POST['submit']
just to make sure :), then change (where we need to) the values GET to values POST ... as some one describe long ago GET works by adding variables to the end of URL's, POST works by sending variables in the body of the request (normal users will not see them). "" It is important to carefully consider which method to use for a certain task. ""

Re: Error code 1064:

Posted: Tue Sep 04, 2012 11:58 pm
by krobinson937
It's been a while but thanks. Been busy with school...I figured I go back to school and learn how to code/html.