我正在使用CodeIgniter并且数据库存在一些问题。我使用表格'客户'中的客户名称填充我的下拉列表。在DB中。我需要从下拉列表中选择名称,输入项目名称并将其添加到数据库表项目中,但在此表中,我应该没有客户名称,但是ID。我该如何实现呢?当我想发送不是id,但是名字 - 我有一个错误:customerName有NULL值。
这是我的控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Add extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url', 'form'));
$this->load->model("addmodel");
$this->load->library('session');
}
private function view($page, $data=false) {
$this->load->view("header.php", $data);
$this->load->view($page, $data);
$this->load->view("footer.php", $data);
}
public function add_project() {
$this->load->database();
$data['customers']=$this->addmodel->select_customer();
$this->view('add/add_project.php', $data);
}
public function new_project() {
$this->load->database();
$data['customers']=$this->addmodel->select_customer();
$this->load->view("add/add_project.php", $data);
$projectName = $this->input->post("projectName");
$customerID = $this->input->post("customerName");
$this->addmodel->add_project($projectName, customerID);
redirect("add_project", "refresh");
}
}
这是我的模特
<?php
class AddModel extends CI_Model {
public function __construct() {
$this->load->database();
}
function add_project($projectName, $customerID) {
$this->db->insert('projects', array(
'projectName' => $projectName,
'customerID' => $customerID
));
}
function select_customer() {
$query = $this->db->get('customers');
return $query;
}
}
?>
最后,这是我的观点
<?php echo form_open('new_project'); ?>
<div id="form-main">
<div id="form-div">
<form class="form" id="form1">
<select class="feedback-input" id="customer_selecter">
<option name="customerName">Select customer</option>
// <?php
foreach($customers->result() as $row){
echo '<option value="'.$row->customerID.'">'.$row->customerName.'</option>';
}
?>
</select>
<p class="projectName">
<input type="text" name="projectName" placeholder="Project name" required class="feedback-input" id="projectName" />
</p>
<div class="submit">
<button type="submit" id="button-red">Add</button>
<div class="ease"></div>
</form>
</div>
</form>
</div>
答案 0 :(得分:0)
将选项值更改为$ row-&gt; customerName,以便您可以获取客户名称
<select class="feedback-input" id="customer_selecter" name="customerName>
// <?php
foreach($customers->result() as $row){
echo '<option value="'.$row->customerID.'">'.$row->customerName.'</option>';
}
?>
</select>
答案 1 :(得分:0)
更改您的Html代码选择框名称,如下所示:
<select class="feedback-input" id="customer_selecter" name="customerName">
<option >Select customer</option>
// <?php
foreach($customers->result() as $row){
echo '<option value="'.$row->customerID.'">'.$row->customerName.'</option>';
}
?>
</select>
并且需要使用$
为customerId传递corrct变量,如下所示:
$this->addmodel->add_project($projectName, $customerID);