how can i send a value from a drop-down menu to the DB table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

how can i send a value from a drop-down menu to the DB table

Post by crazytopu »

the following code works fine in inserting all the field values to the database table except the category vaue.

how can i send a value from a drop-down menu toe the DB table?

Code: Select all

<html> 

<body> 
<?php 

if (isset($_POST['submit'])) { 



// process form 

$db = mysql_connect("localhost", "root"); 

mysql_select_db("library",$db); 

$sql = "INSERT INTO book (call_no,title,author1,author2,publisher,isbn,price,no_of_page,edition,category) VALUES ('{$_POST['T1']}','{$_POST['title']}','{$_POST['author1']}','{$_POST['author2']}','{$_POST['publisher']}','{$_POST['isbn']}','{$_POST['price']}','{$_POST['no_of_page']}','{$_POST['edition']}','{$_POST['pull']}',)"; 


$result = mysql_query($sql); 

echo "Your data was inserted successfuly\n"; 

} else{ 



// display form 



?>
<table border="0" width="35%">
  <tr>
    <td width="100%"><img border="0" src="file:///f:/web/ibcslarge.png" width="317" height="30"></td>
  </tr>
  <tr>
    <td width="100%">&nbsp;</td>
  </tr>
</table>
<table border="0" width="35%">
  <tr>
    <td width="100%">&nbsp;</td>
  </tr>
  <tr>
    <td width="100%"></td>
  </tr>
</table>
<table border="0" width="100%">
  <tr>
    <td width="100%"><a href="http://">Home&nbsp;</a>&nbsp; <a href="http://">Account&nbsp;&nbsp;</a>&nbsp;
      <a href="file:///F:/my%20project%20work%20(final)/CD_ENTRY_FORM.htm">New
      CD Entry Form&nbsp;</a>&nbsp; <a href="http://">New Journal Entry
      Form&nbsp;</a>&nbsp; <a href="http://">Delete Record</a></td>
  </tr>
</table>
<table border="0" width="35%">
  <tr>
    <td width="100%">&nbsp;</td>
  </tr>
</table>
<table border="0" width="35%" bgcolor="#FFCC66">
  <tr>
    <td width="100%">
      <p align="center"><font size="3">NEW BOOK ENTRY FORM</font></td>
  </tr>
</table>
<table border="0" width="35%">
  <tr>
    <td width="100%">&nbsp;</td>
  </tr>
</table>



<form method="post" action="<?php echo $PHP_SELF?>"> 

<table border="0" width="100%">
  <tr>
    <td width="15%">Call No</td>
    <td width="85%"><input type="text" name="T1" size="20"></td>
  </tr>
  <tr>
    <td width="15%">Book Title</td>
    <td width="85%"><input type="text" name="title" size="20"></td>
  </tr>
  <tr>
    <td width="15%">Author1</td>
    <td width="85%"><input type="text" name="author1" size="20"></td>
  </tr>
  <tr>
    <td width="15%">Author2</td>
    <td width="85%"><input type="text" name="author2" size="20"></td>
  </tr>
  <tr>
    <td width="15%">Publisher</td>
    <td width="85%"><input type="text" name="publisher" size="20"></td>
  </tr>
  <tr>
    <td width="15%">ISBN</td>
    <td width="85%"><input type="text" name="isbn" size="20"></td>
  </tr>
  <tr>
    <td width="15%">Price</td>
    <td width="85%"><input type="text" name="price" size="20"></td>
  </tr>
  <tr>
    <td width="15%">No of Page</td>
    <td width="85%"><input type="text" name="no_of_page" size="20"></td>
  </tr>
  <tr>
    <td width="15%">Category</td>
    <td width="85%"><select size="1" name="pull">
        <option selected>Choose a Category</option>
        <option>Programming</option>
        <option>Databases</option>
        <option>Networking</option>
        <option>Operating Systems</option>
        <option>Algorithm</option>
      </select></td>
  </tr>
</table>
<p> 

&nbsp;<input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="reset"> 


</p>


</form> 



<?php 



} // end if 



?> 



</body> 



</html>
[Edit: Added PHP tags for eyecandy. --JAM]
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

What errors do you get? Looks allright from here.
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post by lazersam »

I tested your script.

Its seems Ok to me - the $pull is collecting the data. Try removing the comma from the last sql query.
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

It does not give me any error msg. It just does not insert any value into a new row.

all the string type fields are empty except all the neumeric type fileds where mysql put '0' by default.

so it looks like:

call_no author1.....price.............
0 ..... .................0....................0
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

It does not give me any error msg. It just does not insert any value into a new row.

all the string type fields are empty except all the neumeric type fileds where mysql put '0' by default.

so it looks like:

call_no author1.....price.............
0 ..... .................0....................0

which coma to remove lazersam? ididnot get u properly.
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

this is the code that works fine without the pull down manu stuff.

<html>

<body>
<?php

if (isset($_POST['reset'])) {

echo "just a min..we will make it work soon ;-)";

}


else if (isset($_POST['submit'])) {



// process form

$db = mysql_connect("localhost", "root");

mysql_select_db("library",$db);

$sql = "INSERT INTO book (call_no,title,author1,author2,publisher,isbn,price,no_of_page,edition) VALUES ('{$_POST['T1']}','{$_POST['title']}','{$_POST['author1']}','{$_POST['author2']}','{$_POST['publisher']}','{$_POST['isbn']}','{$_POST['price']}','{$_POST['no_of_page']}','{$_POST['edition']}')";


$result = mysql_query($sql);

echo "Your data was inserted successfuly\n";

} else{



// display form



?>
<table border="0" width="35%">
<tr>
<td width="100%"><img border="0" src="file:///f:/web/ibcslarge.png" width="317" height="30"></td>
</tr>
<tr>
<td width="100%">&nbsp;</td>
</tr>
</table>
<table border="0" width="35%">
<tr>
<td width="100%">&nbsp;</td>
</tr>
<tr>
<td width="100%"></td>
</tr>
</table>
<table border="0" width="100%">
<tr>
<td width="100%"><a href="http://">Home&nbsp;</a>&nbsp; <a href="http://">Account&nbsp;&nbsp;</a>&nbsp;
<a href="file:///F:/my%20project%20work%20(final)/CD_ENTRY_FORM.htm">New
CD Entry Form&nbsp;</a>&nbsp; <a href="http://">New Journal Entry
Form&nbsp;</a>&nbsp; <a href="http://">Delete Record</a></td>
</tr>
</table>
<table border="0" width="35%">
<tr>
<td width="100%">&nbsp;</td>
</tr>
</table>
<table border="0" width="35%" bgcolor="#FFCC66">
<tr>
<td width="100%">
<p align="center"><font size="3">NEW BOOK ENTRY FORM</font></td>
</tr>
</table>
<table border="0" width="35%">
<tr>
<td width="100%">&nbsp;</td>
</tr>
</table>



<form method="post" action="<?php echo $PHP_SELF?>">

<table border="0" width="100%">
<tr>
<td width="15%">Call No</td>
<td width="85%"><input type="text" name="T1" size="20"></td>
</tr>
<tr>
<td width="15%">Book Title</td>
<td width="85%"><input type="text" name="title" size="20"></td>
</tr>
<tr>
<td width="15%">Author1</td>
<td width="85%"><input type="text" name="author1" size="20"></td>
</tr>
<tr>
<td width="15%">Author2</td>
<td width="85%"><input type="text" name="author2" size="20"></td>
</tr>
<tr>
<td width="15%">Publisher</td>
<td width="85%"><input type="text" name="publisher" size="20"></td>
</tr>
<tr>
<td width="15%">ISBN</td>
<td width="85%"><input type="text" name="isbn" size="20"></td>
</tr>
<tr>
<td width="15%">Price</td>
<td width="85%"><input type="text" name="price" size="20"></td>
</tr>
<tr>
<td width="15%">No of Page</td>
<td width="85%"><input type="text" name="no_of_page" size="20"></td>
</tr>
<tr>
<td width="15%">Edition</td>
<td width="85%"><input type="text" name="edition" size="20"></td>
</tr>
</table>
<p>

&nbsp;<input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="reset">


</p>


</form>



<?php



} // end if



?>



</body>



</html>




but once i added the pull down menu feature and added another column in my DB table Book - category...i m unable to send data to any of those fields.
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post by lazersam »

Code: Select all

<?php

$sql = "INSERT INTO book (call_no,title,author1,author2,publisher,isbn,price,no_of_page,edition,category) VALUES ('{$_POST['T1']}','{$_POST['title']}','{$_POST['author1']}','{$_POST['author2']}','{$_POST['publisher']}','{$_POST['isbn']}','{$_POST['price']}','{$_POST['no_of_page']}','{$_POST['edition']}','{$_POST['pull']}')"; 

?>
Like that without the end comma. If that doesnt work then you should check your database setup. Make sure the field type is correct.
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post by lazersam »

Im pretty sure its that comma!
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post by crazytopu »

And i m 110% sure that it was the coma :lol:

Thanks lazersam. It worked fantastic this time...

can u give me a sample code that will ask the user to input username and password and let them to accessh book entry form?

Or, if you know any such site that has a code like this..please forward the link. It would be a great help to me if u do.

Thanks for ur effort again
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post by lazersam »

Look up sessions. I would put their username and password into a db table then create a sign_up form that submits to a handler that inserts the information into the table.

Then create a Sign IN form for them to use. Submit this to a handler that checks the username and password with the db entry. If valid set a variable (eg $auth="ok") and get the session to check for $auth in your protected pages. ;)
Post Reply