我製作了一個頁面,用戶可以從一個表格(表格數據)中搜索數據,然後將搜索結果輸入到另一個表格(表格XY)中。我試圖實現的是當搜索結果出現時,它出現在另一個表單中,並且當第二個表單被提交時,數據被插入到表XY中。但到目前爲止,我所做的只能實現第一個目標,即從表數據中搜索數據。當驗證運行False時,錯誤似乎完成了表單,但是當驗證運行爲True時,稍後檢查時數據不在XY表格中。沒有錯誤通知。我不知道我在哪裏做錯了。這裏是我的代碼: 控制器Data.php:Codeigniter Form結果作爲另一個表單輸入值。第二種形式沒有做它應該做的事
public function index(){
$this->load->view('form');
}
public function search_xy(){
$this->form_validation->set_rules('id', 'ID', 'required|numeric|exact_length[16]');
if($this->form_validation->run() == false) {
$this->index();
} else {
$this->m_data->search_xy();
$this->index();
}
}
public function insert_xy(){
$this->form_validation->set_rules('id', 'ID', 'required');
$this->form_validation->set_rules('xname', 'X Name', 'required|alphanumeric');
$this->form_validation->set_rules('yname', 'Y Name', 'required|alphanumeric');
$this->form_validation->set_rules('xage', 'X Age', 'required');
$this->form_validation->set_rules('yage', 'Y Age', 'required');
$this->form_validation->set_rules('children', 'Children', 'required|numeric');
if($this->form_validation->run() == false) {
$this->index();
} else {
$this->m_xy->insert_xy();
$this->session->set_flashdata('message', '<div class="alert alert-success" data-dismiss="alert">Insert Data to XY Table Success!</div>');
$this->index();
}
}
型號m_data.php:
public function search_pus(){
$id = $this->input->post('id');
$this->db->empty_table('searched', TRUE);
$this->db->query("INSERT INTO 'searched' SELECT a.id, a.name as xname, a.age as xage, b.name as yname, b.age as yage FROM searched a, searched b WHERE a.gender = 1 AND b.gender = 2 AND a.id = '$id' AND b.id = '$id'");
return $this->db->query("SELECT * FROM searched")->result_array();
}
型號m_xy.php:
public function insert_xy(){
$data = array(
'id' => $this->input->post('id'),
'xname' => $this->input->post('xname'),
'yname' => $this->input->post('yname'),
'xage' => $this->input->post('xage'),
'yage' => $this->input->post('yage'),
'children' => $this->input->post('children'));
$this->db->insert('XY', $data);
}
查看form.php的:
<?php $attributes = array("class" => "form-inline"); echo form_open('data/search_xy', $attributes);?>
<fieldset>
<div class="form-group">
<label class="control-label" for="id">Enter ID :</label>
<input type="text" name="id" class="form-control" id="id" value="<?php echo set_value('id'); ?>" />
<button class="btn btn-info btn-md" type="submit"> Search </button><span class="text-danger"><?php echo form_error('id');?></span>
</div>
</fieldset>
<?php echo form_close(); ?>
<?php echo form_open('data/insert_xy'); ?>
<fieldset>
<div class="form-group">
<h4 class="text-left text-primary">Search Result:</h4>
<table style="font-size:11px;" class="table table-condense table-bordered table-hover display" cellspacing="0" width="100%">
<thead>
<tr class="primary">
<th></th>
<th>X Name</th>
<th>Y Name</th>
<th>X age</th>
<th>Y age</th>
</tr>
</thead>
<tbody>
<?php foreach($search_result as $row): ?>
<tr>
<td><input type="checkbox" class="form-control" name="no_kk" id="no_kk" value="<?php echo $row['no_kk'];?>" /></td>
<td><input class="form-control" name="xname" value="<?php echo $row['xname'];?>" /></td>
<td><input class="form-control" name="yname" value="<?php echo $row['yname'];?>" /></td>
<td><input class="form-control" name="xage" value="<?php echo $row['xage'];?>" /></td>
<td><input class="form-control" name="yage" value="<?php echo $row['yage'];?>" /></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<div class="col-md-3">
<select class="form-control" name="children" value="<?php echo set_value('children');?>">
<option value="">-- How Many Children? --</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value=">3">3 or more</option>
</select>
<span class="text-danger"><?php echo form_error('children'); ?></span>
</div>
<div class="col-md-3">
<input class="btn btn-primary btn-md" type="submit" value="Save" />
</div>
</div>
</fieldset>
<?php echo form_close() ?>
當我點擊「保存到XY」我希望爲我檢查表XY要插入的價值..
是不是因爲形式是一張桌子?
我不確定,但在第二種形式中,您有一個循環,您可以在其中循環使用相同名稱的輸入字段。你應該使用''。當你想將它插入到PHP到DB,你必須遍歷你的輸入帖子,以獲得他們的價值觀。 – Iamzozo
@Izozozo呃我不這麼想過。謝謝,我會在此搜索。 –