我有一个向客户发送批量消息的表单,点击提交按钮后,它会在发送前将消息保存在数据库中,但是插入过程需要大约 2 分钟才能插入 3000 条记录,我怎样才能减少插入时间或者我怎样才能在后台处理数据,以避免用户等待处理完成。我尝试了几种有关堆栈溢出的选项,但没有成功。我在共享主机上。这是我的代码
<?php
if(isset($_POST['submit'])){
$date = date("D, F d, Y h:i:sa");
$phones = $_POST['recipient_phones']; //get phone numbers from textarea
$phones = trim($phones,",\r\n\t\r\n/\s+/\0\x0B]/"); //do some regex
$phones = multiexplode(array(","," ","\n","r\n",".","|",":"),$phones);//reformat and convert to array
$sender = $_POST['sender_id']; //Get Sender ID input field
$message = $_POST['message']; //Get Message input field
$time = $_POST['sc_time']; //Get time input field
ob_end_clean();
ignore_user_abort();
ob_start();
header("Connection: close");
// echo json_encode($out);
header("Content-Length: " . ob_get_length());
ob_end_flush();
flush();
foreach($phones as $phone){
$data = array("sender" => "$sender","phone" => "$phone", "message" => "$message", "user_id" => "$user_id","time_submitted" => "$date");
$qry = Insert('crbsms_queue',$data);
$_SESSION['msg']="10";
echo "<script>location.href='$url?success=yes';</script>";
exit
}
# Insert Data
function Insert($table, $data){
global $mysqli;
//print_r($data);
$fields = array_keys( $data );
$values = array_map( array($mysqli, 'real_escape_string'), array_values( $data ) );
//echo "INSERT INTO $table(".implode(",",$fields).") VALUES ('".implode("','", $values )."');";
//exit;
mysqli_query($mysqli, "INSERT INTO $table(".implode(",",$fields).") VALUES ('".implode("','", $values )."');") or die( mysqli_error($mysqli) );
}
Cats萌萌