I make a page where a user can search a data from a table(table Data) to then input the search result into another table(table XY). What I am trying to achieve is when the search result appear, it appears inside another form, and when the second form is submitted, the data is inserted to table XY. But so far what I made can only achieve the first goal, which is searching data from table Data. When validations run False the errors appear to complete the form, but when the validations run True, the data is not in the table XY when I checked it later. No error notifications. I don't know where do I do wrong. Here is my code: Controller Data.php:
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();
}
}
Model 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();
}
Model 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);
}
View 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() ?>
This the view printscreen incase you don't get what I meant:
When I click "Save to XY" I wish for the value I checked be inserted in table XY..
Is it because the form is in a table?