Jump to content

Insert multiple rows in mysqli prepared statement


Go to solution Solved by Barand,

Recommended Posts

I do have 3 mysql tables "users", "banks", and "user_bank". Every users must have at least one bank and maximum is two.

When users signup to system I need to insert bank details to `user_bank` table also.

This is how I tried it:
 

    $success=FALSE;
    
    if($success == FALSE) {
        $query = "INSERT INTO user_bank (
                                      beneficiary_id
                                    , beneficiary_bank_id
                                    , branch_id
                                    ) VALUES (?, ?, ?)";
        $stmt = $mysqli->prepare($query);            
        $stmt->bind_param('iii', $lastInsertId
                               , $bank_one
                               , $branchId_one
                         );    
        $stmt->execute();
                
        if ($stmt->affected_rows == 1) {                        
            $success=TRUE;
            
            if(!empty($bank_two) && !empty($branchId_two)) {
                
                $query = "INSERT INTO user_bank (
                                                id
                                                , bank_id
                                                , branch_id
                                                ) VALUES (?, ?, ?)";
                $stmt = $mysqli->prepare($query);            
                $stmt->bind_param('iii', $lastInsertId
                                       , $bank_two
                                       , $branchId_two
                                 );    
                $stmt->execute();    
                $success=TRUE;
            }
        }
    }
    
    if ($success == TRUE) {
        $messages = array('success'=>true, 'message'=>'You successfully registered.');
    }  

My question is, can anybody tell me I there a way to use a single query for this? If so how its doing?

Thank you.

When using prepared statements in a situation like this you define query, prepare and bind parameters once only.

 

After doing that, loop through the data values to set the parameters and execute.

$beneficiary = 123;          // or whatever
$bank_data = array (
            $branchId1 ,
            $branchId2    
);

$sql = "INSERT INTO user_bank (beneficiary_id, branch_id VALUES (?,?))";
$stmt = $db->prepare($sql);
$stmt->bind_param('ii', $benficiary, $branch);

foreach ($bank_data as $branch) {
    $stmt->execute();
}
Edited by Barand
typo
  • Solution

then

$beneficiary = 123;          // or whatever
$bank_data = array (
            array($branchId1 , $acc1),
            array($branchId2 , $acc2)   
);

$sql = "INSERT INTO user_bank (beneficiary_id, branch_id, account VALUES (?,?,?))";
$stmt = $db->prepare($sql);
$stmt->bind_param('ii', $benficiary,$branch, $acc);

foreach ($bank_data as $bdata) {
    list($branch, $acc) = $bdata;
    $stmt->execute();
}
  • Like 1
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.