>我有一个来自Codeigniter的错误,它是:
"遇到 PHP 错误
严重性:通知
消息:未定义的索引:域"它不是唯一一个我想使用 id 访问的所有数据都返回相同的错误,但有自己的 id......
我尝试获取表单的值(带有 POST)并将它们放入数据库中。但是,因此我无法访问这些值。我刚刚尝试使用硬编码值,我能够将其记录在数据库中。
在这里,我的视图代码是销售书籍的一种形式:
<form method="post" action="<?php echo 'http://localhost:8888/index.php/sell/addBook/'?>">
<label for="title">Title</label>
<input type="text" class="form-control" value="" id="title" placeholder="Title">
<label for="author">Author</label>
<input type="text" class="form-control" value="" id="author" placeholder="Author">
<label for="subject">Subject</label>
<input type="text" class="form-control" value="" id="subject" placeholder="Subject">
<label for="price">Price</label>
<input type="float" class="form-control" value="" id="price" placeholder="Price">
<label for="code">Course Code</label>
<input type="text" class="form-control" value="" id="coursecode" placeholder="Course Code">
<label for="domain">Please select a Domain</label>
<select id="domain" class="form-control">
<?php
$domain = $this->db->get('Domain');
foreach ($domain->result() as $row){
echo " <option value='"".$row->name."'">";
echo $row->name;
echo "</option>";
}?>
</select>
<label for="coursename">Course Name</label>
<select id="coursename" class="form-control">
<?php
$domain = $this->db->get('Course');
foreach ($domain->result() as $row){
echo " <option value='"".$row->name."'">";
echo $row->name;
echo "</option>";
}?>
</select>
<label for="university">University</label>
<select id="university" class="form-control">
<?php
$domain = $this->db->get('Course');
foreach ($domain->result() as $row){
echo " <option value='"".$row->college."'">";
echo $row->college;
echo "</option>";
}?>
</select>
<label for = "selectCondition">Select the Condition of the Book</label> <button type="button" class="btn btn-default btn-xs">
<span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>
</button>
<select id="condition" class="form-control">
<option value="mint">Mint</option>
<option value="very good">Very Good</option>
<option value="good">Good</option>
<option value="fair">Fair</option>
<option value="poor" selected="true">Poor</option>
</select>
<br>
<label for="exampleInputFile">Upload Photo</label>
<input type="file" id="exampleInputFile">
<p class="help-block">Upload photos to assure the buyer your book is in good condition</p>
<button type="submit" class="btn btn-default">Submit</button>
</form>
这里是我的控制器的代码
<?php
class sell extends CI_Controller
{
public $user = "";
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
// Store user information and send to profile page
public function index()
{
$this->load->view('sell');
}
public function addBook()
{
$query1 = "SELECT id FROM Domain WHERE name=" . $_POST['domain']. ";";
/*$this->db->select('id');
$this->db->from('Domain');
$this->db->where('name =', $_POST['domain'));*/
$domid = $this->db->query($query1);
$query2 = "SELECT id FROM Course WHERE name=" . $_POST['coursename'] . " AND college=" + $_POST['university'] . ";";
$course = $this->db->query($query2);
$this->db->set('title', $_POST['title']);
$this->db->set('author', $_POST['author']);
$this->db->set('price', intval($_POST['price']));
$this->db->set('subject', $_POST['subject']);
$this->db->set('coursecode', $_POST['coursecode']);
$this->db->set('cond', $_POST['condition']);
$this->db->set('domainid', intval($domid));
$this->db->set('courseid', intval($course));
$this->db->set('sellerid', $_SESSION['id']);
$this->db->insert('Book');
}
}
?>
谢谢你的帮助!
您只需要将域字段的名称属性添加为:
<select id="domain" class="form-control">
应该是:
<select id="domain" class="form-control" name="domain">
如果域字段正在获取字符串值,那么还需要在SELECT Statement
域字段周围使用引号,如下所示:
$query1 = "SELECT id FROM Domain WHERE name='".$_POST['domain']."'";