0

I have created a bootstrap form ,but it won't connect to the database. After filling up the forms, it gives me an undefined index error. I have no idea what to do. Can you help me? I'm so lost.

<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>

The undefined index error refers to Book_Number etc.

 <?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);
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Arci
  • 11
  • 1
  • 1
  • 3
  • Show the exact error please. Also, this might be a duplicate. – Marc Giroux Sep 07 '16 at 14:31
  • You are vulnerable to [sql injection attacks](http://bobby-tables.com), and `'$Y ear_Published` inyour query isn't exactly valid. `$Y` is an undefined variable. – Marc B Sep 07 '16 at 14:32
  • Do a var_dump() on the $_POST[] array. – Iain Sep 07 '16 at 14:33
  • `mysql_*` functions are deprecated. More of all, you directly assign $_POST data to vars, without checkin if they exists, checkout documentation for `isset`. More of all, your sql query is more open to injections than a drug addict. Please checkout documentation & tutorials about `PDO` or `mysqli`. – Bobot Sep 07 '16 at 14:36
  • Nah. It's not a duplicate. :P – Arci Sep 07 '16 at 14:47

3 Answers3

0

None of your form elements have name attributes, so the form isn't actually posting anything to the server.

Add names:

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

In an HTML form, the key/value pairs posted to the server are build from the names and values of the form elements.

David
  • 208,112
  • 36
  • 198
  • 279
0

With forms to send data to the next page your inputs need to have the name attribute. try changing the id = 'book_number' to name = 'book_number'

James
  • 406
  • 1
  • 3
  • 12
0

You will need a name= attribute for your form elements for the posted values to show up in the $_POST[] array

Iain
  • 387
  • 2
  • 12