首先我的代码在下面
$address_value_array = array($_POST['rta_pic_pc'], $_POST['rta_pic_house']);
foreach ($address_value_array as $value) {
Test::save(array(null, $value, null, $value));
}
在我的系统中,用户可以为我的一个客户端配置文件提供多个地址,因此在保存时我必须检查用户是否输入了一个两个或两个以上的地址,我应该如何使用foreach循环那种将价值输入数据库的场合。
我已尝试使用$ key值设置,但会收到错误。
$address_value_array = array($_POST['rta_pic_pc'], $_POST['rta_pic_house']);
foreach ($address_value_array as $key => $value) {
Test::save(array(null, $value[$key], null, $value[$key]));
}
如果您可以帮助我如何将这些值插入数据库
btw rta_pic_pc =邮政编码,rta_pic_house =房屋号
亲切的问候
答案 0 :(得分:1)
我认为像这样你也把$ key,但我不确定:
$address_value_array = [];
array_push ( $address_value_array , $_POST['rta_pic_pc'], $_POST['rta_pic_house'] );
答案 1 :(得分:1)
鉴于您的评论,我并不知道您的保存功能期望什么,如何:
Test::save(array(null, $_POST['rta_pic_pc'], null, $_POST['rta_pic_house']));
答案 2 :(得分:0)
大家好,我找到了解答的方法
我做了什么,因为我有一个隐藏的变量,存储已经保存的地址的id我用来循环然后捕获循环中的值如果没有意义让我把代码放在这里
HTML:
<?php
$counter = 1;
while ($person_address = $person_addresses->fetch(PDO::FETCH_OBJ)) {
$address_row = Address::findByID($person_address->address_id);
?>
<div class="row">
<div class="col-md-6">
<div class="form-group" id="div_pic_pc<?php echo $counter; ?>">
<label class="control-label">Postcode<span class="required" aria-required="true" > * </span></label>
<input type="hidden" name="rta_pic_person_add_id[]" value="<?php echo $address_row->id; ?>" />
<input type="text" value="<?php echo $address_row->postcode; ?>" name="rta_pic_pc<?php echo $counter; ?>" id="rta_pic_pc<?php echo $counter; ?>" class="form-control input-sm" />
</div>
</div>
<div class="col-md-6">
<div class="form-group" id="div_pic_house<?php echo $counter; ?>">
<label class="control-label">House Number<span class="required" aria-required="true" > * </span></label>
<input type="text" value="<?php echo $address_row->house_no; ?>" name="rta_pic_house<?php echo $counter; ?>" id="rta_pic_house<?php echo $counter; ?>" class="form-control input-sm" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group" id="div_pic_flat<?php echo $counter; ?>">
<label class="control-label">Flat Number</label>
<input type="text" value="<?php echo $address_row->flat_no; ?>" name="rta_pic_flat<?php echo $counter; ?>" id="rta_pic_flat<?php echo $counter; ?>" class="form-control input-sm" />
</div>
</div>
<div class="col-md-6">
<div class="form-group" id="div_pic_street<?php echo $counter; ?>">
<label class="control-label">Street</label>
<input type="text" value="<?php echo $address_row->street; ?>" name="rta_pic_street<?php echo $counter; ?>" id="rta_pic_street<?php echo $counter; ?>" class="form-control input-sm" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group" id="div_pic_town<?php echo $counter; ?>">
<label class="control-label">Town</label>
<input type="text" value="<?php echo $address_row->town; ?>" name="rta_pic_town<?php echo $counter; ?>" id="rta_pic_town<?php echo $counter; ?>" class="form-control input-sm" />
</div>
</div>
<div class="col-md-6">
<div class="form-group" id="div_pic_county<?php echo $counter; ?>">
<label class="control-label">County</label>
<input type="text" value="<?php echo $address_row->county; ?>" name="rta_pic_county<?php echo $counter; ?>" id="rta_pic_county<?php echo $counter; ?>" class="form-control input-sm" />
</div>
</div>
</div>
<?php
$counter++;
endwhile;
?>
PHP:
$counter = 1;
foreach ($_POST['rta_pic_person_add_id'] as $value) {
$postcode = htmlspecialchars($_POST['rta_pic_pc' . $counter]);
$houseno = htmlspecialchars($_POST['rta_pic_house' . $counter]);
$flatno = htmlspecialchars($_POST['rta_pic_flat' . $counter]);
$street = htmlspecialchars($_POST['rta_pic_street' . $counter]);
$town = htmlspecialchars($_POST['rta_pic_town' . $counter]);
$county = htmlspecialchars($_POST['rta_pic_county' . $counter]);
Address::save(array($value, $flatno, $houseno, $street, $town, $postcode, $county));
$counter++;
}