thara Posted November 28, 2015 Share Posted November 28, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/299596-insert-multiple-rows-in-mysqli-prepared-statement/ Share on other sites More sharing options...
Barand Posted November 28, 2015 Share Posted November 28, 2015 (edited) 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 November 28, 2015 by Barand typo Quote Link to comment https://forums.phpfreaks.com/topic/299596-insert-multiple-rows-in-mysqli-prepared-statement/#findComment-1527262 Share on other sites More sharing options...
thara Posted November 28, 2015 Author Share Posted November 28, 2015 Then how to use bank_ids? It also may have different values. Quote Link to comment https://forums.phpfreaks.com/topic/299596-insert-multiple-rows-in-mysqli-prepared-statement/#findComment-1527263 Share on other sites More sharing options...
Barand Posted November 28, 2015 Share Posted November 28, 2015 If your bank_branch table stores the bank_id then you don't need it in user_bank table. Quote Link to comment https://forums.phpfreaks.com/topic/299596-insert-multiple-rows-in-mysqli-prepared-statement/#findComment-1527264 Share on other sites More sharing options...
thara Posted November 28, 2015 Author Share Posted November 28, 2015 Yes, I understood. We really don't need that bank_id column. But I still have the problem if I have account_number instead of bank_id, Quote Link to comment https://forums.phpfreaks.com/topic/299596-insert-multiple-rows-in-mysqli-prepared-statement/#findComment-1527265 Share on other sites More sharing options...
Solution Barand Posted November 28, 2015 Solution Share Posted November 28, 2015 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(); } 1 Quote Link to comment https://forums.phpfreaks.com/topic/299596-insert-multiple-rows-in-mysqli-prepared-statement/#findComment-1527266 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.