Take the 2-minute tour ×

I am building a mini library management system but ran into a few issues and was hoping I could get some tips and suggestions.

Currently I have two pages: one insert.php, and one HTML page.

When I successfully insert a book or transaction into the database a new page opens up and I receive a success status message.

  1. Is it possible to combine both these pages into one and return the success message below the submit button?

  2. I tried using the following to pull lms_status_types_status_id directly from the database but failed:

    <select  name="lms_status_types_status_id" id="individual" multiple="multiple">
    <option value="0" SELECTED>Select User</option> 
    <?php foreach($RET as $userdata){ ?>
    <option value="<?php echo $userdata['lms_status_types_status_id']; ?>"></option>
    <?php } ?>

Any idea what I am doing wrong here?

I have tried asking Google for help but found it difficult to implement what was floating around. I am entirely new to php so please excuse me!



if (!$con)

$RET = DBGet(DBQuery("SELECT * FROM lms_transactions")); 

mysql_select_db("my_db", $con);

$sql="INSERT INTO lms_books (book_id, tag, title, author, year)
('$_POST[book_id]','$_POST[tag]','$_POST[title]' ,'$_POST[author]' ,'$_POST[year]')";

$sql="INSERT INTO lms_transactions (student_id, lms_books_tag, lms_books_book_id, lms_status_types_status_id)

if (!mysql_query($sql,$con))
die('Error: ' . mysql_error());
echo "1 record added";



<h2> Library Management System</h2>

<h3>Check-In/Check-Out Book:</h3>
<form action="/insert.php" method="post">
Student ID: <input type="text" name="student_id">
Book Tag: <input type="text" name="lms_books_tag">
Book ISBN: <input type="text" name="lms_books_book_id">
Status: <select name="lms_status_types_status_id">
    <option value="Checked-In">Checked-In</option>
    <option value="Checked-Out">Checked-Out</option>
<input type="submit">

<h3>Insert Book:</h3>
Book ISBN: <input type="text" name="book_id">
Tag: <input type="text" name="tag"> 
Author: <input type="text" name="author">
Year: <input type="text" name="year">
<input type="submit">
share|improve this question
Your code has SQL injection issues. It also looks like you are using a couple of different database access methods. –  datasage Jan 20 '13 at 3:59
@datasage Yes I've mashed this all up from a couple of places. I will clean it out and try posting a more consistent and revised version. Thanks! –  junaidkaps Jan 20 '13 at 5:56

1 Answer 1

You are declaring the variable $sql twice, which means that when it comes to the mysql_query() function, only the second query string will ever be used.

Yes, it is possible to combine the HTML and the PHP on the same page. You can just put the PHP above the HTML and wrap it in an if($_POST){} (or if(!$_POST){}, depending on what you're trying to do). You then have the form submit to its own page.

Also, you should clean your POST inputs before inserting them into your database or you are opening yourself up to security risks as datasage mentions.

share|improve this answer
I didn't realize the $sql issue till you pointed it out. Could you guide me in the right direction on how to post multiple queries and call them? Meanwhile, I will try and post a revised and more consistent version as per your and datasage's comments. Appreciate it! –  junaidkaps Jan 20 '13 at 6:00
OK, very briefly, you need to: 1. Identify which form has been submitted - you might do that by if($_POST['student_id']{ //code for producing sql statement for the first form } elseif($_POST['book_id']{ //code for producing sql statemnt for second form } else { //whatever } 2. Then generate your sql query based on the statement you produced above. You might like to use different conditions in the if statements. You could even pass a unique variable in a hidden form input to determine which form has been submitted. –  Chid Gilovitz Jan 20 '13 at 6:05

Your Answer


