我正试图在两个不同的数据库中写入数据。我有一些数据用户必须插入,但其他数据用户无法插入。
对于这些数据,我应该检查它们是否已插入,如果数组不为空,我应该将这些数据插入数据库,否则不插入。
所以我的代码是:
//this is the part that the user must insert.
$meeting = [
'motivation' => $this->post('motivation'),
'date' => $this->post('date'),
'start_at' => $this->post('start_at'),
'end_at' => $this->post('end_at'),
];
$meeting_id = $this->Meeting_model->post($meeting);
var_dump($meeting);
//this is the part that user should not insert.
$companions = [
'name' => $this->post('name'),
'surname' => $this->post('surname'),
'fiscal_code' => $this->post('fiscal_code'),
];
//So there I should control if the array is empty.
if(empty($companions))
{
var_dump($companions);
foreach ($companions as $postCompanion) {
$dataCompanion = [
'name' => $postCompanion['name'],
'surname' => $postCompanion['surname'],
'fiscal_code' => $postCompanion['fiscal_code'],
'meeting_id' => $meeting_id,
];
$companions_id = $this->Companion_model->post($dataCompanion);
$this->response(['companions_id', $companions_id]);
}
}
我的问题是,通过这种方式,无论我是否传递数据,我都不会在"if"中输入。。我以前尝试过:$companions != []
但如果用户不插入数据,结果会是:
'name' => boolean false
'surname' => boolean false
'fiscal_code' => boolean false
所以我在数据库中写下了我的:VALUES(0,0,0(
我该如何解决这个问题?非常感谢。
错误是因为$companions
从不为空,它已经有键了。这应该对你有用——↓↓
//this is the part that the user must insert.
$meeting = [
'motivation' => $this->post('motivation'),
'date' => $this->post('date'),
'start_at' => $this->post('start_at'),
'end_at' => $this->post('end_at'),
];
$meeting_id = $this->Meeting_model->post($meeting);
var_dump($meeting);
//this is the part that user should not insert.
$companions = array();
if($this->post('name') != "" && $this->post('surname') != "" && $this->post('surname') != ""){
$companions = [
'name' => $this->post('name'),
'surname' => $this->post('surname'),
'fiscal_code' => $this->post('fiscal_code'),
];
}
//So there I should control if the array is empty.
if(empty($companions))
{
var_dump($companions);
foreach ($companions as $postCompanion) {
$dataCompanion = [
'name' => $postCompanion['name'],
'surname' => $postCompanion['surname'],
'fiscal_code' => $postCompanion['fiscal_code'],
'meeting_id' => $meeting_id,
];
$companions_id = $this->Companion_model->post($dataCompanion);
$this->response(['companions_id', $companions_id]);
}
}
您的变量$companions
作为数组从未为空,因为您已经在其中设置了键(当然,值取决于$this->post()
的结果(。
如果你有固定的3个数据$this->post('name'), $this->post('surname'), $this->post('fiscal_code')
,你只需要检查其中的3个,就像这样。
$companions = [
'name' => $this->post('name'),
'surname' => $this->post('surname'),
'fiscal_code' => $this->post('fiscal_code'),
];
//So there I should control if the array is empty.
if(!empty($companions['name']) && !empty($companions['surname']) && !empty($companions['fiscal_code']))
{
$companions['meeting_id'] = $meeting_id;
$companions_id = $this->Companion_model->post($companions);
$this->response(['companions_id', $companions_id]);
}
$partners不为空。请查看此代码。
<?php
$companions = [
'name' => NULL,
'surname' => NULL,
'fiscal_code' => NULL,
];
//DON'T IUSE THIS.
if(empty($companions))
{
echo 'EMPTY : ';
var_dump($companions);
}else{
echo 'NOT EMPTY : ';
var_dump($companions);
}
// USE THIS.
if(empty($companions['name']) && empty($companions['surname']) && empty($companions['fiscal_code'])){
echo 'EMPTY : ';
die(var_dump($companions));
}else{
echo 'NOT EMPTY : ';
die(var_dump($companions));
}