我使用foreach循环从控制函数调用模型函数,以便将值插入数据库。但该功能只插入一行。 这是我的控制功能:
public function insert()
{
if(!isset($_REQUEST['UserContacts']))
{
echo json_encode('No Data Uploaded');
exit;
}
// if(!isset($_REQUEST['userIndex']))
// {
// echo json_encode('userIndex not provided');
// exit;
// }
$contactList =$this->model->getContact();
$userIndex=1;
$contact=null;
foreach($contactList as $contact)
{
echo "inside foeach";
$this->model->fields = array();
echo "post contact no".$contact['phone_number']."<br>";
array_push($this->model->fields, $contact['phone_number']);
array_push($this->model->fields, $contact['contact_name']);
array_push($this->model->fields, 'aaa@ff.cc');
array_push($this->model->fields, $userIndex);
$rowcount = $this->model->insertcontactsync();
echo $rowcount;
}
}
这是我的模型功能代码:
public function insertcontactsync()
{
$query1 = "INSERT INTO `contact_sync`(`userIndex`, `contactName`, `contactEmail`, `contactNumber`,`status`, `contactCreatedOn`, `contactModifiedOn`) VALUES ('".$this->fields[3]."','".$this->fields[1]."','".$this->fields[2]."',".$this->fields[0].",'no match','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
//print_r($query1);
echo "<br>";
$result = $this->connect->query($query1);
echo $result;
}