Click to See Complete Forum and Search --> : SQL Syntax Error


chesemonkyloma
09-05-2006, 06:25 PM
It keeps giving me an error, saying that my SQL syntax is incorrect. Here is the complete code of the section, in case it's needed, but just pay attention to the SQL. I didn't post this on another forum because it could also be a PHP problem.


$modelname=ucwords(strtolower($_POST[ModelName]));
$creatorname=ucwords(strtolower($_POST[CreatorName]));
$id=substr(md5(uniqid(rand(), true)), 0, 6);
$imageType=substr(strrchr($_FILES['Picture']['name'], "."), 1);
$imagePath="images/models/".$modelname.".".$imageType;
$link=mysql_connect("localhost", "username", "password")
or die('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("sockigam_", $link);

$sql="INSERT INTO Models (Name, Creator, Email, Description, Category, Socks, NoOfSteps, Difficulty, ImageType, ID)
VALUES ('$modelname', '$creatorname', '$_POST[Email]', '$_POST[Description]', '$_POST[Category]', $_POST[Socks], $_POST[StepsNumber], '$_POST[Difficulty]', '$imageType', '$id')";
mysql_query($sql, $link) or die(mysql_error());

move_uploaded_file($_FILES["Picture"]["tmp_name"], "..".$imagePath);
echo "<FORM ACTION=\"$_SERVER[PHP_SELF]\" METHOD=\"POST\">
<TABLE>";
for ($i=1; $i<=$_POST[StepsNumber]; $i++)
{
echo "<TR>
<TD>Step $i:</TD>
<TD><TEXTAREA ROWS=2 COLS=30 NAME=\"Step{$i}\"></TEXTAREA></TD>
</TR>";
}
echo "<INPUT TYPE=\"hidden\" NAME=\"Model\" VALUE=\"{$modelname}\"><TR><TD><INPUT TYPE=\"Submit\" NAME=\"Submit2\" VALUE=\"Submit\"></TD></TR>
</TABLE></FORM>";

chazzy
09-05-2006, 06:29 PM
can you post the exact error message...

chesemonkyloma
09-05-2006, 06:31 PM
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 ' , 'Blank', '', '7236c7')' at line 2

NogDog
09-05-2006, 06:57 PM
Try echoing the query as part of your die() message, and then you'll probably see that some value in the value list is not getting set the way you think it is:

mysql_query($sql, $link) or die("Query failed: $sql - " . mysql_error());

Then you can look at the part of the code that sets/retrieves that variable and figure out what went wrong.

chesemonkyloma
09-05-2006, 07:10 PM
it messes up when the form is empty, thats all, any way i can fix that? i know i can do an if statement if the variables from the form are empty, will that work?

chazzy
09-05-2006, 07:45 PM
that depends on what you need - can the form fields be empty or do you require input?

chesemonkyloma
09-05-2006, 08:03 PM
ummm they all need input except one for email and picture, which im not sure which ones require input my sql, but they DO require input for my purposes, so im not sure, but you don't have to try now ill figure something out but im going to bed soon, ill be back tomorrow with some ideas

chazzy
09-05-2006, 08:29 PM
well, it's typically bad form to reference arrays without constant member names..otherwise variables... so maybe you could replace many of the members with something like


$email = (isset($_POST['email'])) ? $_POST['email'] : "email default" ;


Basically this means set $email to $_POST['email'] if isset($_POST['email']) evaluates to true, otherwise set it to "email default"