Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys! I need your help ! I'm doing a form in PHP and a want to display in my dropdownlist name column from my DB and save the name_id of that name selected from dropdownlist in DB. Here is my code.
PHP
if(isset($_POST["submit"]))
	{
	$bookshop_id = mysql_real_escape_string($_POST["bookshop_id"]);
	$book_id = mysql_real_escape_string($_POST["book_id"]);
	$quantity = mysql_real_escape_string($_POST["quantity"]);
		
mysql_query("INSERT INTO `books_bookshop`(`bookshop_id`,`book_id`,`quantity`)
             VALUES ('" . $bookshop_id . "','" . $book_id . "','" . $quantity . "')") 
or die ("Book could not be registered.");
		
}
	
?>
<form method="post" action="#">
<label for="bookshop_id">Bookshop Name</label>
<select style="width:148px;" id="bookshop_id" name="bookshop_id" value="bookshop_id">
<option>
<?php echo $row["name"]; ??>
</option>
<?php } ??>
</select>


<table><tbody><tr><td><select name="book" id="book" value="book">
<?php $query = mysql_query("SELECT `name` FROM `books`");<br mode="hold" /?>while ($row = mysql_fetch_array($query)){?>
<option><?php echo $row["name"]; ??></option>
<?php } ??>
</select>
<table><tbody><tr>
<td><input type="submit" name="submit" value="Register book"></input></td>
</tr></tbody></table>

</td></tr></tbody></table></form>
Posted
Updated 28-May-14 23:12pm
v7

You have not explained the work flow. I have to figure it out from your incomplete code.
If I understand you correctly, you are trying to retrieve a list of books from book table, populate a dropdownlist, then, on the form, user selects a bookshop from the bookshop dropdownlist and selects a book from the book dropdownlist, press submit, and the bookshop_id, book_id, and quantity got inserted into the books_bookshop table.
There are too many problems here to be able to help you. You do not seem to know the code, neither php nor html, where did you get it from? Getting code from nowhere and trying to piece them together without understanding them and hoping that it works out of the box rarely happens. Giving you the code is not going to help you to learn, I would rather give you the logic.
1. Where do you get the bookshop information to populate the bookshop dropdownlist? You will populate bookshop name as text for user to see but the the most important piece is the bookshop_id that is required to insert into the books_bookshop table
2. You have to retrieve book_id, book_name, may be quantity from the book table, populate the book dropdownlist with book_name as text, and the other two as hidden field.
3. When the user select a bookshop and book from the respective dropdownlists, press submit, it is the bookshop_id, book_id, and quantity that will be sent to the receiving page, it appears that it is a postback to the same page and they are captured in the $_POST variables like $+POST['bookshop_id']
Suggest you learn these:
1. php_forms.asp[^]
2. You should go for mysqli as mysql is deprecated, also use prepared statement to prevent sql injection. Refer: prepared-statements-in-php-and-mysqli[^]
 
Share this answer
 
v5
Thanks guys! I have solved this problem by myself !
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900