Jump to content

Error in sql syntax


Imri-Persiado
Go to solution Solved by Ch0cu3r,

Recommended Posts

That's the error I get:

 

 

Error3: 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 'sprint spikes'', ''Nike'', '' at line 2

For the following code:

function post($type, $brand, $gender, $size, $hand, $isNew, $price, $desc, $imgname, $name, $userid, $phone)
{
   $con=connect();
   if (!$con)
      die('Could not connect: ' . mysql_error());
   // Check connection
   $type       = check_input   ($type);
   $brand   = check_input   ($brand);
   $gender  = check_input   ($gender);
   $size      = check_input   ($size);
   $hand     = check_input   ($hand);
   $price     = check_input   ($price);
   $desc     = check_input   ($desc);
   $imgname = check_input   ($name);
   $userid  = check_input   ($usid);
   $name     = check_input   ($name);
   $phone     = check_input   ($phone);
   
   $date = date("Y/m/d");
   $sql="INSERT INTO spikes (type, brand, gender, size, hand, new, price, description, imgname, date, name, userid, phone)
   VALUES('$type', '$brand', '$gender', 
               '$size', '$hand', '$isNew', 
               '$price', '$desc', '$imgname', 
               '$date', '$name', '$userid', 
               '$phone')";

   if (!mysql_query($sql,$con)){
      die('Error3: ' . mysql_error($con));
   }
   
   mysql_close($con);
}
function check_input($value)
{
   // Stripslashes
   if (get_magic_quotes_gpc())
      $value = stripslashes($value);

   // Quote if not a number
   if (!is_numeric($value))
      $value = "'" . mysql_real_escape_string($value) . "'";

   return $value;
}

I really don't understand what I'm doing wrong..

Link to comment
Share on other sites

  • Solution

Your check_input() function is encapsulating values within single quotes and you are also encapsulating each value in the query with single quotes too. In affect your values in the query are wrapped in single quotes twice. This is what is causing the error.

 

To fix the error change $value = "'" . mysql_real_escape_string($value) . "'"; to just $value = mysql_real_escape_string($value);

Link to comment
Share on other sites

I would have taken a different approach! Instead of modifying

if(!is_numeric($value)) $value = "'" . mysql_real_escape_string($value) . "'";

I'd modify

$sql="INSERT INTO spikes (type, brand, gender, size, hand, new, price, description, imgname, date, name, userid, phone)
VALUES($type, $brand, $gender, 
$size, $hand, $isNew, 
$price, $desc, $imgname, 
$date, $name, $userid, 
$phone)";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.