mysql_query() [[url] 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 'into kiwanis_info ( 'identity', 'firstnam', 'lastnam', 'phonenum', 'emailaddr', ' at line 1

Above is waht I get when inserting into a file from a class. The code looks as follows:

function addstate($ent, $errmsg, $identity) {
	var_dump($ent);
	$ent['identity'] = 0;
    echo $query = "INSERT INTO into kiwanis_info ( 'identity', 'firstnam', 'lastnam', 'phonenum', 'emailaddr', 'address1', 'city1', 'state1', 'zipcode1', 'infrequest', 'organ')
	values ('".$ent['identity']."', '".$ent['firstnam']."', '".$ent['lastnam']."', '".$ent['phonenum']."', '".$ent['emailaddr']."',
	'".$ent['address1']."', '".$ent['city1']."', '".$ent['state1']."', '".$ent['zipcode1']."',
    '".$ent['infrequest']."', '".$ent['organ']."')";
	$result1 = mysql_query($query);
    if ($result1) {
	  $this->identity = mysql_insert_id();
	  $identity = $this->identity;
	  $this->errmsg = "Information record $identity was added "; } 
	else  { $this->errmsg = "ERROR ... Information record not added"; }
    return $this->errmsg;
	return $this->identity;
     }

Don Johnston, e-mail: <EMAIL SNIPPED>

Dani AI

Generated

Good catches by (remove the single quotes around identifiers) and (don’t try to insert an auto‑increment key). A few concise, practical additions that clarify the remaining pitfalls and prevent similar problems later.

  • Check the SQL string itself: echo the final query and paste it into the MySQL client to see the exact syntax error.
  • Use mysql_error() (or the exception message with PDO/mysqli) immediately after the query to get the full diagnostic.
  • Identifiers must not be wrapped in single quotes; either leave them unquoted or use backticks (`colname`). Single quotes define string literals and cause syntax errors.
  • If a column is AUTO_INCREMENT, omit it from the column list; let the server assign the value. Consider renaming ambiguous names like identity to id to avoid cross‑DB confusion.
  • If still using the old mysql extension, escape values with mysql_real_escape_string(); better: migrate to prepared statements (PDO or mysqli) to eliminate quoting issues and SQL injection risks.

A minimal PDO pattern to replace manual concatenation:

$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('INSERT INTO mytable (col1, col2, col3) VALUES (:col1, :col2, :col3)');
$stmt->execute([':col1'=>$v1, ':col2'=>$v2, ':col3'=>$v3]);
$insertId = $pdo->lastInsertId();

Also note a function-level bug to fix: a function with two immediate return statements will only ever execute the first. Set properties (error text, id) and return a single value (boolean or the insert id) consistently.

Recommended Answers

All 8 Replies

I also meant to add that the error occurs on $result = mysql_query($query);

Just looking at it real quick is identity your primary key and set for auto-increment? If so then do not attempt to set the value as the system will automatically fill it in.

Thanks I will give it a try

I removed the identity field and even changed $result 1 to $result. I get the following:Warning: mysql_query() [http://www.mysql.com/doc]: 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 'into kiwanis_info ( 'firstnam', 'lastnam', 'phonenum', 'emailaddr', 'address1', ' at line 1 in C:\wamp\www\Hicksville PHP\Hicksville_Infoc.php on line 97
Line 97 is $result = mysql_query($query);

Just read the error message carefully.
And then erase one of the double "INTO into" from your query.

Thanks I can't believe I missed that. I am now getting: Warning: mysql_query() [http://www.mysql.com/doc]: 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 ''firstnam', 'lastnam', 'phonenum', 'emailaddr', 'address1', 'city1', 'state1', '' at line 1 in C:\wamp\www\Hicksville PHP\Hicksville_Infoc.php on line 97. The query is as follows: $query = "INSERT INTO kiwanis_info('firstnam', 'lastnam', 'phonenum', 'emailaddr', 'address1', 'city1', 'state1', 'zipcode1', 'infrequest', 'organ')
values ( '".$ent."', '".$ent."', '".$ent."', '".$ent."',
'".$ent."', '".$ent."', '".$ent."', '".$ent."',
'".$ent."', '".$ent."')";
$result = mysql_query($query);

Do not use the apostroph ' to mark column names. Either drop the apostroph (which I always do) or use the backtick ` for column names.

Thanks Smantscheff. It works I removed the apostrophes and left the names to stand alone. Thanks Again.

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.