Jump to content

Insert multiple recods from php array


bravo14

Recommended Posts

Hi Guys

 

I have a number of arrays that are posted from a form

 

They are

$home_rider

$order

$away_rider

$awayorder

 

for each home rider I want to to action the following query

 

"INSERT INTO tbl_lg_rider_order (`name`,`card_id`,`homeaway`,`riderorder`) VALUES ('$homerider',$card_id,'h','$homeorder')"

 

and for each away rider I want to do the following

 

"INSERT INTO tbl_lg_rider_order (`name`,`card_id`,`homeaway`,`riderorder`) VALUES ('$awayrider',$card_id,'a','$awayorder')"

 

I am using the following code

 

 

$i=0;
for($i){
$homerider=$home_rider[$i];
$homeorder=$order[$i];
$homesql="INSERT INTO tbl_lg_rider_order (`name`,`card_id`,`homeaway`,`riderorder`) VALUES ('$homerider',$card_id,'h','$homeorder')";
echo $homesql;
$i++;
}
$i=0;
for($i){
$awayrider=$away_rider[$i];
$awayorder=$awayorder[$i];
$homesql="INSERT INTO tbl_lg_rider_order (`name`,`card_id`,`homeaway`,`riderorder`) VALUES ('$awayrider',$card_id,'a','$awayorder')";
echo $awaysql;
$i++;
}

 

I am getting an error saying Parse error: syntax error, unexpected ')', expecting ';'

 

Where am I going wrong?

Link to comment
https://forums.phpfreaks.com/topic/291098-insert-multiple-recods-from-php-array/
Share on other sites

You might also want to finish your code and add a query call and a test of its results inside that loop.  Depending on where your input is coming from (the user?) you should also be concerned about security and use prepared queries.  Hopefully you are NOT planning on using MySQL_* functions.

Better to use a foreach() loop

$db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE);

$sql = "INSERT INTO tbl_lg_rider_order (`name`,`card_id`,`homeaway`,`riderorder`) 
        VALUES (?,?,?,?)";
$stmt = $db->prepare($sql);
$stmt->bind_param('siss', $name,$card_id,$ha,$rideorder);

foreach ($home_rider as $k => $name) {
    $ha = 'h';
    $rideorder = $order[$k];
    $stmt->execute();
}
foreach ($away_rider as $k => $name) {
    $ha = 'a';
    $rideorder = $awayorder[$k];
    $stmt->execute();
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.