shailbenq
shailbenq

Reputation: 1470

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`)
)

Upvotes: 3

Views: 116

Answers (3)

ivoputzer
ivoputzer

Reputation: 6469

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());
}

Upvotes: 2

MISJHA
MISJHA

Reputation: 1008

The error seems to be before the table name Scoretab. Did you check your syntax carefully? Sometimes we don't see what's right in front of our eyes! :D Just replicated the example and everything worked for me.

Upvotes: 1

JOE LEE
JOE LEE

Reputation: 1058

try this

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

Upvotes: 1

Related Questions