-1

This has been asked a lot here, but I can't seem to find the problem....

I wanted to submit my FormMail.php to a MySQL database using php.

Getting the error:

Error=Insert failed: 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 ''FullName', 'EmailAddr', 'contact', 'colors', 'vehicles', 'mesg', 'email', 'rnam' at line 1

<?php
$con = mysql_connect("localhost","root","db79ax4");
if (!$con)
{
Error('database','Could not connect: ' . mysql_error()); /* this also exits the script */
}
$FullName = mysql_real_escape_string($aCleanedValues['FullName'],$con);
$EmailAddr = mysql_real_escape_string($aCleanedValues['EmailAddr'],$con);
$contact = mysql_real_escape_string($SPECIAL_VALUES['contact'],$con);
$colors = mysql_real_escape_string($SPECIAL_VALUES['colors'],$con);
$vehicles = mysql_real_escape_string($SPECIAL_VALUES['vehicles'],$con);
$mesg = mysql_real_escape_string($SPECIAL_VALUES['mesg'],$con);
$email = mysql_real_escape_string($SPECIAL_VALUES['email'],$con);
$rname = mysql_real_escape_string($SPECIAL_VALUES['rname'],$con);
mysql_select_db("resourcentr", $con);
$sql="INSERT INTO johnwork ('FullName', 'EmailAddr', 'contact', 'colors', 'vehicles', 'mesg', 'email', 'rname')
VALUES
('$FullName','$EmailAddr','$contact','$colors','$vehicles','$mesg','$email','$rname')";
if (!mysql_query($sql,$con))
{
Error('database','Insert failed: ' . mysql_error()); /* this also exits the script */
}
mysql_close($con);

Any ideas? Is there something wrong with the above code? Thanks!

John
  • 11

2 Answers2

2

You dont need single quotes ' around your column names in INSERT INTO statement.

$sql="INSERT INTO `johnwork` (`FullName`, `EmailAddr`, `contact`, `colors`, `vehicles`, `mesg`, `email`, `rname`)
VALUES
('$FullName','$EmailAddr','$contact','$colors','$vehicles','$mesg','$email','$rname')";
M.Ali
  • 67,945
  • 13
  • 101
  • 127
  • It's good practice to always backtick your field (column) names, so that you don't use an SQL reserved word by accident. Ditto for table names. Single quotes as the OP used, are, in fact, an error. – Phil Perry May 05 '14 at 23:13
  • better practice to know what the reserved words are; erroneous backticks cause more problems than they solve –  May 05 '14 at 23:20
  • Agreed :) ................ – M.Ali May 05 '14 at 23:22
2

Change this:

$sql="INSERT INTO johnwork ('FullName', 'EmailAddr', 'contact', 'colors', 'vehicles', 'mesg', 'email', 'rname')
VALUES
('$FullName','$EmailAddr','$contact','$colors','$vehicles','$mesg','$email','$rname')";

To this:

$sql="INSERT INTO johnwork (FullName, EmailAddr, contact, colors, vehicles, mesg, email, rname)
VALUES
('$FullName','$EmailAddr','$contact','$colors','$vehicles','$mesg','$email','$rname')";

So your code look like this:

<?php
$con = mysql_connect("localhost","root","db79ax4");
if (!$con)
{
Error('database','Could not connect: ' . mysql_error()); /* this also exits the script */
}
$FullName = mysql_real_escape_string($aCleanedValues['FullName'],$con);
$EmailAddr = mysql_real_escape_string($aCleanedValues['EmailAddr'],$con);
$contact = mysql_real_escape_string($SPECIAL_VALUES['contact'],$con);
$colors = mysql_real_escape_string($SPECIAL_VALUES['colors'],$con);
$vehicles = mysql_real_escape_string($SPECIAL_VALUES['vehicles'],$con);
$mesg = mysql_real_escape_string($SPECIAL_VALUES['mesg'],$con);
$email = mysql_real_escape_string($SPECIAL_VALUES['email'],$con);
$rname = mysql_real_escape_string($SPECIAL_VALUES['rname'],$con);
mysql_select_db("resourcentr", $con);
$sql="INSERT INTO johnwork (FullName, EmailAddr, contact, colors, vehicles, mesg, email, rname)
VALUES
('$FullName','$EmailAddr','$contact','$colors','$vehicles','$mesg','$email','$rname')";
if (!mysql_query($sql,$con))
{
Error('database','Insert failed: ' . mysql_error()); /* this also exits the script */
}
mysql_close($con);
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268