shailbenq shailbenq - 5 months ago 13
SQL Question

Error while inserting data into Mysql database

I am trying to insert data into Mysql table, but it is giving me an error as-


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 'Scoretab VALUES ('UX 345','22','0.8562675')' at line 1


This is the php-mysql snippet that im using :

if($value >= 0.70){
$mu_id = $ros['c_id'];
$moc_id = $ram['t_id'];
$query="INSERT INTO Scoretab VALUES ('$mu_id','$moc_id','$value')";

$op1 = mysql_query($query) or die(mysql_error());
}


This is my table structure:

CREATE TABLE IF NOT EXISTS `Scoretab` (
`mu_id` varchar(10) NOT NULL,
`moc_id` int(5) NOT NULL,
`score` decimal(5,4) NOT NULL,
UNIQUE KEY `mu_id` (`mu_id`)
)

Ivo Ivo
Answer

There could potentially be a few problems with this query

$query="INSERT INTO Scoretab VALUES ('$mu_id','$moc_id','$value')";
  • Does the number of columns match the fields your trying to insert? Have you tried using using specific column identifier Scoretab (col,col,col) values (val, val, val)

  • Does any of your values contain an unescaped apostrophe? You might want to consider using mysql_real_escape_string for $mu_id and intval for $moc_id maybe!

  • $value is a float you don't need to ad apostrophes while inserting

  • Are you sure you are connected to the same database you have this table in?

this could be a possible working solution (edit)

if ($value >= 0.70)
{
    $mu_id = mysql_real_escape_string($ros['c_id']);

    $moc_id = intval($ram['t_id']); 

    $query = "INSERT INTO `Scoretab` VALUES ('$mu_id', $moc_id, $value)";

    $op1 = mysql_query($query) or die(mysql_error());
}
Comments