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>
&#13;
<?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');
}
}
?>
&#13;
感谢您的帮助!
答案 0 :(得分:1)
您只需要为域字段添加名称属性:
<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']."'";