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

Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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