我正在尝试将一些表单数据插入到codeigniter中的mysql表中。我的问题是,在插入实际数据之前,会在表中插入一个空行。然后我发现,即使页面加载或刷新,空行也会插入到mysql中。
这是我的代码:
控制器:
class Amc extends MX_Controller {
function __construct()
{
parent::__construct();
// check if user logged in
if (!$this->ion_auth->logged_in())
{
redirect('module=auth&view=login');
}
$this->load->library('form_validation');
$this->load->model('amc_model');
}
function addamc()
{
$data = array('cust_id' => $this->input->post('customer_id'),
'cust_name' => $this->input->post('cname'),
'cust_address' => $this->input->post('addr'),
'contact_person' => $this->input->post('contact_prsn'),
'location' => $this->input->post('loc'),
'email' => $this->input->post('email'),
'prs' => $this->input->post('prs'),
'rating' => $this->input->post('rating'),
'hrs' => $this->input->post('hrs'),
'engine_num' => $this->input->post('engine'),
'model' => $this->input->post('model'),
'amc_from' => $this->input->post('amcfrom'),
'amc_to' => $this->input->post('amcto'),
'expiry_date' => $this->input->post('expd'),
'no_visit_agreed' => $this->input->post('visagree'),
'no_visit_made' => $this->input->post('vismade'),
'date_commence' => $this->input->post('doc'),
'last_oil_ser_date' => $this->input->post('losd'),
'last_visit_date' => $this->input->post('lvd'),
'visit_due' => $this->input->post('visitdue'),
'visit_status' => $this->input->post('visitstatus'),
'svc_status' => $this->input->post('svcstatus'),
'amc_status' => $this->input->post('amcstat'),
'actual_amt' => $this->input->post('actualamt'),
'amt_recieved' => $this->input->post('amtrecieved'),
'amt_due' => $this->input->post('amountdue'),
'payment_method' => $this->input->post('paymentmethod'),
'payment_due_date' => $this->input->post('paymentdue'),
'assigned_to' => 'not assigned'
);
$this->amc_model->addamc($data);
if ( $this->form_validation->run() == true &&$this->amc_model->addamc($data) )
{
$this->session->set_flashdata('success_message', $this->lang->line('amc_added'));
redirect("module=amc", 'refresh');
}
}
模型:
class Amc_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function addamc($data = array())
{
$amcData = array(
'cust_id' => $data['cust_id'],
'cust_name' => $data['cust_name'],
'cust_address' => $data['cust_address'],
'contact_person' => $data['contact_person'],
'location' => $data['location'],
'email' => $data['email'],
'prs' => $data['prs'],
'rating' => $data['rating'],
'hrs' => $data['hrs'],
'engine_num' => $data['engine_num'],
'model' => $data['model'],
'amc_from' => $data['amc_from'],
'amc_to' => $data['amc_to'],
'expiry_date' => $data['expiry_date'],
'no_visit_agreed' => $data['no_visit_agreed'],
'no_visit_made' => $data['no_visit_made'],
'date_commence' => $data['date_commence'],
'last_oil_ser_date' => $data['last_oil_ser_date'],
'last_visit_date' => $data['last_visit_date'],
'visit_due' => $data['visit_due'],
'visit_status' => $data['visit_status'],
'svc_status' => $data['svc_status'],
'amc_status' => $data['amc_status'],
'actual_amt' => $data['actual_amt'],
'amt_recieved' => $data['amt_recieved'],
'amt_due' => $data['amt_due'],
'payment_method' => $data['payment_method'],
'payment_due_date' => $data['payment_due_date'],
'assigned_to' => $data['assigned_to']
);
if($this->db->insert('amc_details', $amcData)) {
return true;
} else {
return false;
}
}
}
查看:
<?php $attrib = array('class' => 'form-horizontal'); echo form_open("module=amc&view=addamc", $attrib);?>
<table class="imagetable" align="center">
<tr>
<td width="180px" align="left" >Customer ID</td>
<td><input type="text" name="customer_id" id="customer_id" /></td>
<td align="center">Name</td>
<td><input type="text" name="cname" id="cname" /></td>
<td align="center">Address</td>
<td><input type="text" name="addr" id="addr" /></td>
</tr>
<tr>
<td>Location</td>
<td><input type="text" name="loc" id="loc" /></td>
<td align="center">Email ID</td>
<td ><input type="text" name="email" id="email" /></td>
<td align="center">Contact Person</td>
<td><input type="text" name="contact_prsn" id="contact_prsn" /></td>
</tr>
<tr>
<td>ENGINE No</td>
<td><input type="text" name="engine" id="engine" /></td>
<td align="center">Model</td>
<td><input type="text" name="model" id="model" /></td>
<td align="center">PRS</td>
<td><input type="text" name="prs" id="prs" /></td>
</tr>
<tr>
<td>Rating</td>
<td><input type="text" name="rating" id="rating" /></td>
<td align="center">HRS</td>
<td><input type="text" name="hrs" id="hrs" /></td>
<td align="center">Expiry Date</td>
<td><input type="text" name="expd" id="expd" /></td>
</tr>
<tr>
<td align="left">AMC From</td>
<td><input type="text" name="amcfrom" id="amcfrom" /></td>
<td align="center">AMC To</td>
<td><input type="text" name="amcto" id="amcto" /></td>
<td align="center">Date of Commencement</td>
<td><input type="text" name="doc" id="doc" /></td>
</tr> <tr>
<td align="left">No.of visit Agreed</td>
<td><input type="text" name="visagree" id="visagree" /></td>
<td align="center">No.of visit made</td>
<td><input type="text" name="vismade" id="vismade" /></td>
<td align="center">Last Oil Service Date</td>
<td><input type="text" name="losd" id="losd" /></td>
</tr>
<tr>
<td align="left">Last Visit Date</td>
<td><input type="text" name="lvd" id="lvd" /></td>
<td align="center">Visit due every after</td>
<td><input type="text" name="visitdue" id="visitdue" /></td>
<td align="center">SVC Status</td>
<td><input type="text" name="svcstatus" id="svcstatus" /></td>
</tr>
<tr>
<td align="left">Visit Status</td>
<td><input type="text" name="visitstatus" id="visitstatus" /></td>
<td align="center">AMC Status</td>
<td><input type="text" name="amcstat" id="amcstat" /></td>
<td align="center">Actual Amount</td>
<td><input type="text" name="actualamt" id="actualamt" /></td>
</tr>
<tr>
<td align="left"> Amount Recieved</td>
<td><input type="text" name="amtrecieved" id="amtrecieved" /></td>
<td align="center">Amount Due</td>
<td><input type="text" name="amountdue" id="amountdue" /></td>
<td align="center">Payment Method</td>
<td><input type="text" name="paymentmethod" id="paymentmethod" /></td>
<td></td>
</tr>
<tr>
<td align="left">Payment Due Date</td>
<td><input type="text" name="paymentdue" id="paymentdue" /></td>
<tr>
<td>
<?php echo form_submit('submit', $this->lang->line("submit"), 'class="btn btn-primary" style="padding: 6px 15px;"'); ?>
</tr>
</tr>
</table>
<?php echo form_close();?>
我是codeigniter的菜鸟,我无法找到代码的错误。任何人都可以帮我这个..
答案 0 :(得分:4)
在控制器中,只有在验证为真时才应调用插入代码。
$this->amc_model->addamc($data);
应该在控制器函数addamc()中进行更改。最终的代码将是
function addamc()
{
$data = array('cust_id' => $this->input->post('customer_id'),
'cust_name' => $this->input->post('cname'),
'cust_address' => $this->input->post('addr'),
'contact_person' => $this->input->post('contact_prsn'),
'location' => $this->input->post('loc'),
'email' => $this->input->post('email'),
'prs' => $this->input->post('prs'),
'rating' => $this->input->post('rating'),
'hrs' => $this->input->post('hrs'),
'engine_num' => $this->input->post('engine'),
'model' => $this->input->post('model'),
'amc_from' => $this->input->post('amcfrom'),
'amc_to' => $this->input->post('amcto'),
'expiry_date' => $this->input->post('expd'),
'no_visit_agreed' => $this->input->post('visagree'),
'no_visit_made' => $this->input->post('vismade'),
'date_commence' => $this->input->post('doc'),
'last_oil_ser_date' => $this->input->post('losd'),
'last_visit_date' => $this->input->post('lvd'),
'visit_due' => $this->input->post('visitdue'),
'visit_status' => $this->input->post('visitstatus'),
'svc_status' => $this->input->post('svcstatus'),
'amc_status' => $this->input->post('amcstat'),
'actual_amt' => $this->input->post('actualamt'),
'amt_recieved' => $this->input->post('amtrecieved'),
'amt_due' => $this->input->post('amountdue'),
'payment_method' => $this->input->post('paymentmethod'),
'payment_due_date' => $this->input->post('paymentdue'),
'assigned_to' => 'not assigned'
);
if ( $this->form_validation->run() == true)
{
$result = $this->amc_model->addamc($data);
if($result == true) {
$this->session->set_flashdata('success_message', $this->lang->line('amc_added'));
redirect("module=amc", 'refresh');
} else {
//insertion failed
}
}
}
只有在验证为真的情况下才能执行insert语句。