views:

44

answers:

3

here my code-

$sql = "INSERT INTO tblpin ('pinId', 'ownerId', 'usedby', 'status') VALUES ";
for($i=0; $i<sizeof($pin); $i++) 
{
if ($i>0)
    {
    $sql .= ", ";
    }
$sql .= "('$pin[$i]', '$ownerid', 'Free', '1')";
}
$sql .= ";";

echo $sql;

mysql_query($sql);
if(mysql_affected_rows() > 0)
{
    echo "done";
}
else
{
    echo "Fail";
}

output: ** INSERT INTO tblpin ('pinId', 'ownerId', 'usedby', 'status') VALUES ('13837927', 'admin', 'Free', '1'), ('59576082', 'admin', 'Free', '1'); Fail

why it is not inserting values when $sql query is right?

+3  A: 

Don't put your column names between quotes.

klennepette
Use backticks for column names -- ie `\`pidID\`, \`ownerId\``
Erik
A: 
INSERT INTO tblpin (pinId, ownerId, usedby, status) VALUES (13837927, 'admin', 'Free', 1), (59576082, 'admin', 'Free', 1);
Salil
+2  A: 

here

INSERT INTO tblpin ('pinId', 'ownerId', 'usedby', 'status') VALUES

you got to use `` or use nothing to define table and rows but you cant use '' instead so the right query will be:

INSERT INTO `tblpin` (`pinId`, `ownerId`, `usedby`, `status`) VALUES ('13837927', 'admin', 'Free', '1'), ('59576082', 'admin', 'Free', '1');

OR:

INSERT INTO tblpin (pinId, ownerId, usedby, status) VALUES ('13837927', 'admin', 'Free', '1'), ('59576082', 'admin', 'Free', '1');
tazphoenix
@taz but if I am not generating sql query dynamically but passing it as it is, it is working fine?? why.
nectar