gpt4 book ai didi

php - 使用 codeigniter 无法将动态输入值正确保存在数据库中

转载 作者:行者123 更新时间:2023-11-29 10:41:04 25 4
gpt4 key购买 nike

以下代码是使用codeigniter框架开发的,用于在数据库中保存4个动态输入数据。但是由于错误,仅第一个输入框的值被保存4次。我已经包含了代码和屏幕截图。有人可以帮我纠正这个问题吗?这就是我向数据库输入值的方式 enter image description here

enter image description here但这就是它在数据库中保存的方式

这是我的 Controller

function error(){
//if ($this->input->post('mytext')) {

$attain = $this->input->post('mytext', true);
$data2=array(); //<-initialize
foreach ($attain as $i => $a) { // need index to match other properties
//append array
$data2[] = array(
'mytext' => $a,
'mytext1' => $a,
'mytext2' => $a,
'mytext3' => $a,
'projectname'=> $this->input->post('projectname'),
);
//for multiple entry in same table


//}
}
$this->db->insert_batch('projectem', $data2);
redirect('Select_ctrl2/ModalAddEmployeesProject');
}

这是我的观点

 <link rel = "stylesheet" type = "text/css" 
href = "<?php echo base_url(); ?>jquery.multiselect.js-master/css/jquery.multiselect.css">

<link rel = "stylesheet" type = "text/javascript"
href = "<?php echo base_url(); ?>jquery.multiselect.js-master/js/jquery-1.5.min.js">

<link rel = "stylesheet" type = "text/javascript"
href = "<?php echo base_url(); ?>jquery.multiselect.js-master/js/jquery.multiselect.js">



<head>
<script>
</script>
<script type="text/javascript">
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID

var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('</br><div class="col-lg-12"><div class="col-lg-3"><input class="input form-control" placeholder="Task Name" name="mytext[]"/></div><div class="col-lg-3"><input class="input form-control" placeholder="Description" name="mytext1[]"/></div><div class="col-lg-3"><input class="input form-control" placeholder="Task Cost" name="mytext2[]"/></div><div class="col-lg-3"><input class="input form-control" placeholder="Employee" name="mytext3[]"/></div><a href="#" class="remove_field">Remove</a></div>'); //add input box
}
});

$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>
</head>


<body>

<div class="col-lg-10">
<div class="panel panel-danger">
<div id="page-wrapper " style="background-color: #f8f8f8">

<div class="container-fluid shadow " >


<div class="row" style="background-color:#46bc99;">
<div class="col-lg-12" >
<h3 class="text-center formheading">
<li class="glyphicon glyphicon-user" style="font-size: 40px; padding-top: 2px;"></li>
ADD EMPLOYEES TO PROJECT
</h3>

</div>

</div>

<br>
<div class="panel-body" align="center" style="width: 960px; color: navy; border: 2px black; padding: 5px;">

<div class="col-lg-12" >

<?php echo form_open('Select_ctrl2/error'); ?>



<div class="form-group">
<select name="projectname" class="input form-control">
<option value="none" selected="selected">Select Project</option>


<?php foreach($projects as $s):?>
<option value="<?php echo $s->projectname?>"><?php echo $s->projectname?></option>
<?php endforeach;?>
</select>
</div>

<div class="input_fields_wrap">
<div class="form-group">
<button type="button" class="btn btn-success add_field_button">Add More Fields</button>
</div>

</div>
<div>

<button name="submit" type="submit" class="btn btn-info">Submit</button>
</div>

<?php echo form_close(); ?>


</div>





</div>

</div>


</body>
</html>

最佳答案

您将在 foreach 的每次迭代中覆盖值,请记住您收到的 mytext、mytext1、mytext2 和 mytext3 作为数组,因此您需要按索引访问每个值:

function error(){
$attain = $this->input->post();
$data2=array(); //<-initialize
for ($i = 0; $i < count($attain['mytext']); $i++) {
//append array
$data2[] = array(
'mytext' => $attain['mytext'][$i],
'mytext1' => $attain['mytext1'][$i],
'mytext2' => $attain['mytext2'][$i],
'mytext3' => $attain['mytext3'][$i],
'projectname'=> $attain['projectname'],
);
//for multiple entry in same table
}
$this->db->insert_batch('projectem', $data2);
redirect('Select_ctrl2/ModalAddEmployeesProject');
}

关于php - 使用 codeigniter 无法将动态输入值正确保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45474128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com