有人可以帮助我,我试图使用codeigniter在一个表格中的2个表中插入数据。但是,我的问题是,数据未正确插入。也许是因为我的数组,因为我希望我的数据插入添加动态输入。请帮帮我
Table questions_bank:
q_id int(11) AutoIncrement
id int(11)
type varchar(100)
q_text varchar(200)
Table answer:
a_id int(11) AutoIncrement
id int(11)
q_id int(11)
a_text varchar
我的控制器: question.php
function questions_data()
{
$data = array(
'q_id' => $this->input->post('q_id'),
'id' => $this->input->post('id'),
'type' => $this->input->post('type'),
'q_text' => $this->input->post('q_text')
);
$data_answer = array(
//'q_id' => $this->input->post('q_id'),
'id' => $this->input->post('id'),
'a_text' => $this->input->post('a_text[]'),
);
$this->questions_data->questions($data,$data_answer);
redirect('question/insert');
}
型号: questions_data.php
public function questions()
{
$data=array(
'q_id'=>$this->input->post('q_id'),
'id'=>$this->input->post('id'),
'type'=>$this->input->post('type'),
'q_text'=>$this->input->post('q_text'),
);
$data_answer = array(
'id' => $this->input->post('id'),
);
$this->db->insert('questions_bank',$data);
$data_answer['q_id'] = $this->db->insert_id();
$this->db->insert('answer',$data_answer);
foreach($_POST['a_text1'] as $a_text1)
{
$this->db->insert('answer',$a_text1);
}
}
我的观点: insert.php
<label class="col-sm-2 control-label">Course</label>
<div class="col-md-4">
<?php
$query = "SELECT * FROM course"; $result = mysql_query($query); ?>
<select name="id" class="form-control m-b"><?php while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
<option value=" <?php echo $line['id'];?> "> <?php echo $line['Topics'];?></option> <?php } ?>
</select>
</div>
</div>
<div class="line line-dashed b-b line-lg pull-in"></div>
<div class="form-group">
<label class="col-sm-2 control-label">Type</label>
<div class="col-md-4">
<select name="type" class="form-control m-b">
<option value="Easy">Easy</option>
<option value="Intermidiate">Intermidiate</option>
<option value="Hard">Hard</option>
</select>
</div>
</div>
<div class="line line-dashed b-b line-lg pull-in"></div>
<div class="form-group">
<label class="col-sm-2 control-label">Question</label>
<div class="col-md-5">
<input type="text" name="q_text" id="q_text" class="form-control" />
</div>
</div>
<div id="box">
<div class="line line-dashed b-b line-lg pull-in"></div>
<div class="form-group">
<label class="col-sm-2 control-label">Answer</label>
<div class="col-md-5">
<input name="a_text1[' + i +'][a_text]" type="text" id="name" class="form-control"></div>
<img src="<?php echo base_url(); ?>assets/add/add.png" width="32" height="32" border="0" align="top" class="add" id="add" />
</div></div></div>
<button type="submit" class="btn btn-sm btn-default">Submit</button
我附上了结果的图像
多行脚本
<script>
$(document).ready(function(){
$('#add').click(function(){
var inp = $('#box');
var i = $('input').size() ;
$('<div id="box' + i +'"><div class="form-group"><label class="col-sm-2 control-label">Answer</label><div class="col-md-5"><input type="text" id="name' + i+ '" class="form-control" name="a_text1[' + i +'][a_text]" /></div><img src="<?php echo base_url(); ?>assets/add/remove.png" align="top" class="add" id="remove" /></div></div></div>').appendTo(inp);
i++;
});
答案 0 :(得分:0)
//Note: Please change your model function with the below function and when the form is submitted the form values will be posted to controller not to the model you to just pass it to the model function that all.
public function questions($data,$data_answer)
{
$this->db->insert('questions_bank',$data);
$data_answer['q_id'] = $this->db->insert_id();
$this->db->insert('answer',$data_answer);
//Note: For loop case just pass the array from the controller and use it in model
//foreach($_POST['a_text1'] as $a_text1)
//{
//$this->db->insert('answer',$a_text1);
//}
}