gpt4 book ai didi

php - Codeigniter- 即使在页面加载或刷新时也将空白行插入到 mysql 中

转载 作者:太空宇宙 更新时间:2023-11-03 10:49:07 24 4
gpt4 key购买 nike

我正在尝试将一些表单数据插入到 codeigniter 中的 mysql 表中。我的问题是,在插入实际数据之前,表格中插入了一个空白行。然后我发现,即使页面加载或刷新时,也会将空白行插入到 mysql 中。

这是我的代码:

Controller :

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&amp;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 的菜鸟,我找不到代码有什么问题。谁能帮我解决这个问题..

最佳答案

在您的 Controller 中,只有当验证为真时,您才应调用插入代码。

$this->amc_model->addamc($data);

我应该在 Controller 函数 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
}
}
}

仅当验证为真时,才必须执行插入语句。

关于php - Codeigniter- 即使在页面加载或刷新时也将空白行插入到 mysql 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26945117/

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