HI Dani,
I am working on a dissertation project, based on a library management systems.
There are a number of features as admin, that needs to operate, which are as follows:-

  1. Inserting new books' info
  2. Issuing books
  3. Fines
  4. Editing books details
  5. Return books
  6. Multisession logins
    But i'll start for now with the 1st one, i.e. inserting books
    With my Wamp Server installation, i am using these versions, mysql vr. 5.1.36, php vr. 5.3.0, apache vr. 2.2.2.11 with wamp vr. 2.0
    I have a database titled 'automated_library', with a number of tables, for this 1st problem, when i make a new entry , it is supposed to insert to the 'books' table, and at the same the same time, copy those details to another table, 'newentries'. But the problem i am having is, only the books table, is getting the new data, and no entry inserted into the 'newentries' table.

A. The book entry page's code is as follows:-

  <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="../hans/admin/style/stylesheet.css" /> <link rel="stylesheet" href="../hans/admin/style/includes/jquery/jquery-ui-custom.css" /> <script  src="../media/includes/jquery/jquery-ui-custom.css"></script> <script src="../media/includes/bootstrap/css/bootstrap.css"> </script> <link  href="../style/font-awesome.css" /> <script>
jQuery(function($) {
  $("#opens_window").click(function(e) {
      e.preventDefault();       
      $('#dialog').dialog();
  });
});
</script> <title>Admin Panel - Add New Books</title> <style type="text/css"> <!--
#Layer1 {
    position:absolute;
    width:57px;
    height:27px;
    z-index:2001;
    left: 654px;
    top: 29px;
}
.style1 {color: #FF0000}
#Layer2 {
    position:absolute;
    width:354px;
    height:390px;
    z-index:2002;
    left: 274px;
    top: 114px;
}
body {
    background-color: #33FFFF;
}
.style4 {color: #00FFFF}
.style5 {color: #FFFFFF }
#Layer3 {
    position:absolute;
    width:200px;
    height:23px;
    z-index:2003;
    left: 278px;
    top: 85px;
    background-color: #FFFFFF;
}
.style9 {
    color: #666666;
    font-weight: bold;
}
.style10 {
    color: #000000;
    font-weight: bold;
}
.style11 {color: #000000; }
.style12 {color: #CCCCCC}
--> </style> </head> <body> <div class="top_container"> <span id="panel_name">Admin Panel</span> <span id="user">Welcome ! </span> <div id="Layer1"><u><a href="../logout.php" class="style1">Log Out</a> </u></div> <span> </span></div> <ul class="style4" id="menu" name="menu"> <li> <div align="center" class="style11"><strong><a href="getfrombooks.php"> VIEW </a> </strong></div> <li class="style5"> <div align="center"><a href="booksearch.php"><strong> SEARCH  </strong></a> </div> <li class="style9"> <div align="center" class="style5"> <div align="center"><a href="addnew_books.php"> <span class="style12">ADD NEW BOOKS</span> </a> </div> </div> <li class="style5"> <div align="center" class="style5"><a><strong>ISSUED</strong></a> </div> <li class="style5"> <div align="center" class="style11"><strong>FINES </strong> </div> <li> <div align="center"><span class="style10"><a href="adminoptions.php"> ADMIN PANEL</a></span></div> </ul> <div align="justify"> <div id="Layer3"> <div align="center"><strong>Book Addtion Panel </strong></div> </div> <p> </p> <p> </p> <form id="SearchBooks" name="SearchBooks" method="post" action="book_addition.php"> <p> </p> <p> <label></label> </p> <div id="Layer2"><strong>ISBN</strong> <label> <input name="ISBN" type="text" id="ISBN" size="22" maxlength="30" /> </label> <p><strong>Book title
                  : </strong> <input name="Book_title" type="text" id="Book_title" size="20" maxlength="50" /> </p> <p><strong> Author:</strong> <input name="Author" type="text" id="Author" size="20" maxlength="50" /> </p> <p><strong>Year of publication</strong> <label> <input name="yr_of_publication" type="text" id="yr_of_publication" size="6" maxlength="4" /> </label> </p> <p> <label><strong>Edition</strong> <input name="Edition" type="text" id="Edition" size="20" maxlength="50" /> </label> </p> <p><strong>Module</strong> <label> <input name="Module" type="text" id="Module" size="20" maxlength="50" /> </label> </p> <p><strong>Department</strong> <select name="Department" size="1"> <option value="Applied Accounting">Applied Accounting</option> <option value="Business Administration">Business Administration</option> <option value="Financial Services">Financial Services</option> <option value="Information systems">Information systems</option> <option value="Public Sector">Public Sector</option> </select> </p> <p> <label> <input type="submit" name="Submit" value="Submit" /> </label> <input type="reset" name="Submit2" value="Reset" /> </p> </div> <p> <label></label> </p> <p> <label></label> <label></label> </p> </form> <p> </p> <p> </p> <p> </p> <p> <!--B. the form should call book_addition.php when submitting, the code is as follows:, pls note these are separate php pages, i've just contiunued the code here, due to the posting rules, the 2nd php page, i.e.book_addition.php starts here--> 
                  <?php 
$con=mysqli_connect("localhost","root","","automated_library"); 
// Check connection 
if (mysqli_connect_errno()) 
  { 
  echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
  } 
 $sql="INSERT INTO books(ISBN,Book_title,Author,yr_of_publication,Edition,Module,Department) VALUES('$_POST[ISBN]','$_POST[Book_title]','$_POST[Author]','$_POST[yr_of_publication]','$_POST[Edition]','$_POST[Module]','$_POST[Department]')";
 if(!mysqli_query($con,$sql))
 {
 echo "error";
 }
 else
 {
 echo "Success !!";
    //to call another page to show success comment!
          header("Location: addnew_books.php");
  } 
 mysqli_close($con); 
 ?> <?php 
$con=mysqli_connect("localhost","root","","newentries"); 
// Check connection 
if (mysqli_connect_errno()) 
  { 
  echo "Failed to connect to MySQL: " .  mysqli_connect_error();  
  } 
$sql="INSERT INTO newentries.new_entries(ISBN,Book_title,Author,yr_of_publication,Edition,Module,Department) VALUES('$_POST[ISBN]','$_POST[Book_title]','$_POST[Author]','$_POST[yr_of_publication]','$_POST[Edition]','$_POST[Module]','$_POST[Department]')";
 if(!mysqli_query($con,$sql))
  {
          echo "Error inserting data!";
 }
          echo "Success !!";  
    //to call another page to show success comment!
          header("Location: addnew_books.php");
          mysqli_close($con);
          /*I have tried several ways but it only keeps inserting to one table.Where am I wrong?Any help? Thanks.*/
  ?> 

Recommended Answers

All 2 Replies

Looking at your code the new_entries table is in another database (newentries). Is that what you really wanted to do? You are duplicating rows (althoug in another database) so maybe you first review your DB design.
If that is OK, make sure connection to the newentries database is OK and the query is also OK. You can check the error log. In WAMP typical locations might be C:\wamp\logs or C:\wamp\php\logs. You might find some clues there. You can also echo the query out by putting the following simple debug code after line 78:

die($sql);

This will echo your query on screen and stop the script. You can copy the query and paste it into phpmyadmin to test it (make sure you select the newentries database first).
The associative indices of the $_POST array elements should be enclosed in quotes to avoid possible errors. So $_POST[ISBN] should really be $_POST['ISBN']. This might require that you escape some quotes. Also array elements within a double stringed qoute need to be enclosed curly braces to be parsed OK. So the code might look like this:

$sql="INSERT INTO newentries.new_entries(ISBN,Book_title,Author,yr_of_publication,Edition,Module,Department) VALUES('{$_POST[\"ISBN\"]}', '{$_POST[\"Book_title\"]}', '{$_POST[\"Author\"]}', '{$_POST[\"yr_of_publication\"]}', '{$_POST[\"Edition\"]}', '{$_POST[\"Module\"]}', '{$_POST[\"Department\"]}')";

And a warning: using POST variables directly in a query is a bad practice. You should cleanse/filter/sanitize/ those variables first so you do not get some nasty data in.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.