如何将引导程序表单连接到数据库

时间:2016-09-07 14:29:42

标签: php mysql sql phpmyadmin

我创建了一个引导程序表单,但它不会连接到数据库。填写表单后,它给出了一个未定义的索引错误。我不知道该怎么做。你能帮助我吗?我迷路了。

<form name="input" method="post" action="addtodatabase.php">


<form>
<div class="form-group">
  <label for="Book_Number">Book Number</label>
  <input type="text" class="form-control" id="Book_Number"  placeholder="Enter Book Number">
</div>

 <div class="form-group">
  <label for="Book_Title">Book Title</label>
  <input type="text" class="form-control" id="Book_Title" placeholder="Enter Book Title">
</div>

 <div class="form-group">
  <label for="Book_Description">Book Description</label>
  <input type="text" class="form-control" id="Book_Description" placeholder="Enter Book Description">
</div>

 <div class="form-group">
  <label for="Author">Author</label>
  <input type="text" class="form-control" id="Author" placeholder="Enter Author">
</div>

 <div class="form-group">
  <label for="Publisher">Publisher</label>
  <input type="text" class="form-control" id="Publisher" placeholder="Enter Publisher">
 </div>

 <div class="date">
  <label for="Year_Published">Year Published</label>
  <input type="date" class="form-control" id="Year_Published" placeholder="Enter Year Published">
 </div>

<br>    
<input type="submit">   <input type="reset"></center>
</form>

未定义的索引错误是指Book_Number等。

 <?php
 $connection = mysql_connect ('localhost','root','');
mysql_select_db('ABC_Library');

$Book_Number =      $_POST ['Book_Number'];
$Book_Title =       $_POST ['Book_Title'];
$Book_Description=  $_POST['Book_Description'];
$Author =           $_POST ['Author'];
$Publisher  =       $_POST['Publisher'];
$Year_Published =   $_POST['Year_Published'];


$query = 
"INSERT INTO Theology_Books
(Book_Number,Book_Title,Book_Description,Author,Publisher,Year_Published)

values

('$Book_Number','$Book_Title','$Book_Description','$Author','$Publisher','$Y    ear_Published')";

$result = mysql_query($query);
Echo "Database Saved"; 
mysql_close($connection);
?>

3 个答案:

答案 0 :(得分:0)

您的所有表单元素都没有name属性,因此表单实际上并未向服务器发布任何内容。

添加名称:

<input type="text" name="Book_Number" class="form-control" id="Book_Number"  placeholder="Enter Book Number">
                   ^----- here -----^

在HTML表单中,发布到服务器的键/值对是根据表单元素的name和值构建的。

答案 1 :(得分:0)

使用表单将数据发送到下一页,您的输入需要具有name属性。尝试将id = 'book_number'更改为name = 'book_number'

答案 2 :(得分:0)

您的表单元素需要name=属性,以便在$_POST[]数组中显示已发布的值