2013-02-08 50 views
1

我有幾個需要插入數據庫的大表單。使大的Mysqli插入效率更高

我目前做這樣

$stmt = $memberMysqli->prepare("INSERT INTO questionnairebespokeexerciseplan(userid,date,naturalbodytype,framesizewrist,framesizeelbow,fitnessgoals,workpattern,sleephours,runningbarriers,workcommute,workdistance,smoke,biomechanicalissues,limitingillness,reletivesheartproblems,birthlast12months,recurringinjuries,regularmedication,pregnant,anythingelsehealth,gymmember,runoutdoors,wouldyouliketorunoutdoors,homeequipments,whattypeequipment,exercisetime,heartratemonitor,restingheartrate,actualheartrate,maxheartrate,heartratesteadyrun,heartratebreathless,trainingshoes,preferredexercisetype,exerciseclasses,classdays,bike,bikemodel,runlunchtimebeforework,clubmember,clubschedule,personaltraining,otherheartrate,foottype) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); 

$stmt->bind_param('ssssssssssssssssssssssssssssssssssssssssssss', 
               $_POST['userid'], 
             $date, 
             $_POST['naturalbodytype'], 
             $_POST['framesizewrist'], 
             $_POST['framesizeelbow'], 
             $_POST['fitnessgoals'], 
             $_POST['workpattern'], 
             $_POST['sleep'], 
             $_POST['runbarriers'], 
             $_POST['workcommute'], 
             $_POST['workmiles'], 
             $_POST['smoke'], 
             $_POST['biomechanical'], 
             $_POST['illnesses'], 
             $_POST['heartcondition'], 
             $_POST['givenbirth'], 
             $_POST['injuries'], 
             $_POST['medication'], 
             $_POST['pregnant'], 
             $_POST['anythingelse'], 
             $_POST['gymmember'], 
             $_POST['runoutdoors'], 
             $_POST['liketorunoutside'], 
             $_POST['homeequipment'], 
             $_POST['equipment'], 
             $_POST['besttime'], 
             $_POST['heartratemonitor'], 
             $_POST['restingheartrate'], 
             $_POST['actualheartrate'], 
             $_POST['actualmaxheartrate'], 
             $_POST['heartratesteadyrun'], 
             $_POST['heartratebreathless'], 
             $_POST['trainingshoes'], 
             $_POST['preferredexersize'], 
             $_POST['classes'], 
             $_POST['classdays'], 
             $_POST['bike'], 
             $_POST['whatbike'], 
             $_POST['beforeworklunchtime'], 
             $_POST['clubmember'], 
             $_POST['clubschedule'], 
             $_POST['personaltrainer'], 
             $_POST['otherheartrate'], 
             $_POST['foottype'] 
             ); 

,如果有這樣做的更好/更直接的方式,我只是在想什麼?

由於目前需要一些時間來創建和調試它,當事情不正確時也同樣耗時。

回答

1

你可以自己寫一個腳本來自動生成這個語句。 請務必保持字段名稱一致(在你的表格和數據庫),然後也許一塊像這樣的代碼可以爲你工作:

// ADD ALL YOUR FIELDS TO THIS ARRAY. 
// THIS SHOULD BE THE ONLY PART OF THE CODE 
// WHICH YOU WILL HAVE TO MAINTAIN. 

$fields = array(
'userid', 
'date', 
'naturalbodytype', 
'framesizewrist' //etc. add all your fields 
); 

//you may have to adjust some fields manually: 

$_POST['date'] = $date; 


//HERE IS WHERE THE AUTOMATED PART STARTS 
//YOU WONT EVER HAVE TO TOUCH THIS PART 

//writing all fields for the query 
$all_fields = implode(', ' $fields); 

//writing the correct number of questionmarks  
$questionmarks = str_repeat('?, ' count($fields)-1).'?'; 

//generate your statement 
$stmt = $memberMysqli->prepare(
"INSERT INTO questionnairebespokeexerciseplan 
({$all_fields}) 
VALUES 
({$questionmarks})"); 

//write the correct amount of 's' characters for parameter types 
$parameter_types = str_repeat('s' count($fields)); 


//generate the code which will write the parameters 

$all_fields_to_bind=array(); 

foreach($fields as $field) 
{ 
    $all_fields_to_bind[] = "\$_POST['". $field. "']"; 
} 

//generate the code to be evaluated 
$eval_statement = '$stmt->bind_param($parameter_types,'.implode(', ', $all_fields_to_bind).');'; 

//evaluate (meaning execute) your dynamically generated code 
eval($eval_statement); 

我沒有測試這個,有可能仍然是一些語法錯誤。我也不知道它有多安全!但它應該工作,因爲我正在使用類似的SELECT語句。

+0

Eval ?! Yikes ... – 2013-03-25 19:40:07

+0

正如在PHP手冊上看到的,83%喜歡:http://www.php.net/manual/en/mysqli-stmt.fetch.php#107034 – olli 2013-03-25 20:12:06

-1

SafeMysql可以使您的代碼縮短3倍。儘管列出所有允許的字段是必不可少的。

$allowed = explode(",","userid,date,naturalbodytype,framesizewrist,framesizeelbow,fitnessgoals,workpattern,sleephours,runningbarriers,workcommute,workdistance,smoke,biomechanicalissues,limitingillness,reletivesheartproblems,birthlast12months,recurringinjuries,regularmedication,pregnant,anythingelsehealth,gymmember,runoutdoors,wouldyouliketorunoutdoors,homeequipments,whattypeequipment,exercisetime,heartratemonitor,restingheartrate,actualheartrate,maxheartrate,heartratesteadyrun,heartratebreathless,trainingshoes,preferredexercisetype,exerciseclasses,classdays,bike,bikemodel,runlunchtimebeforework,clubmember,clubschedule,personaltraining,otherheartrate,foottype"); 

$data = $db->filterArray($_POST,$allowed); 
$sql = "INSERT INTO questionnairebespokeexerciseplan SET ?u"; 
$db->query($sql, $data);