views:

160

answers:

3

I am creating an application where I am generating pins dynamically based on user's input and storing them into mySql database.

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

how can I do that?

A: 

Something like

$sql = sprintf( "INSERT INTO `tblpin` (`pinId`, `ownerId`, `usedby`, `status`) VALUES ('%s', '%s', '%s', '%s')",
    generatePIN($pin),
    mysql_real_escape_string($ownerId),
    mysql_real_escape_string($usedBy),
    mysql_real_escape_string( $status) );

or (edited for Conspicuous Compiler)

$pins = generatePINS($user); // ? however they're generated
foreach( $pins as $pin) {
    $sql = sprintf( "INSERT INTO `tblpin` (`pinId`, `ownerId`, `usedby`, `status`) VALUES ('%s', '%s', '%s', '%s')",
        $pin,
        mysql_real_escape_string($ownerId),
        mysql_real_escape_string($usedBy),
        mysql_real_escape_string( $status) );
        $result = mysql_query($sql);
}

where generatePIN is your function to make your pin based on whatever the heck you're basing it off of. or generatePINS returns an array of them

Dan Heberden
So, two things: (1) He's trying to do multiple rows of inserts at once with a single SQL statement. Your solution doesn't do that, I don't think. (2) Any answer should note that the asker's given code is ripe for SQL injection attacks. (Your proposed solution has this same flaw.)
Conspicuous Compiler
The OP makes no notes of multiple rows - the question is vague. Pins could be a typo, one column, who knows what. the OP makes no mention if he/she is sanitizing his data or not; if the OP does any search re: sql on this site, he/she is sure to find plenty of examples (hell, even some of my answers with tips for him/her)
Dan Heberden
+1  A: 

Try this:

$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')";
}

Of course you need to escape the values of $pin in case they contain any characters which could mess with the SQL query.

cherouvim
strings in php are concatenated with "." and ";" in the end of query for mysql_query() is a wrong char due to it accepts one and only one query. and the latest: `foreach` is more handy in this case.
zerkms
thanks zerkms. It's been 5 years...
cherouvim
It is not working `echo $sql;` is displaying `0`.
nectar
@nectar: I had a couple of typos which I corrected.
cherouvim
@cherouvim :ya I got that , final `$sql` is coming fine but whet I call `mysql_query($sql);` , it is not inserting data into db.why?
nectar
@nectar: is there an error? if you do echo $sql, does the statement look good?
cherouvim
+1  A: 
$s = $pdo->prepare("INSERT INTO xy (a,b,c,d) VALUES (?,?,?,?)");
foreach ($pins as $i) {
   $s->execute($i,$ownerID,"free",1);
}
mario
i bet OP used bulk insert for some performance reasons, but this would work too.
zerkms