472,334 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,334 software developers and data experts.

how to insert value from select box into database?

hye.

i'm new here.

in php coding as well.

sorry in advance if my questions are stupid question.

but i just can't find the solution even after googled it.

here is my question.

i'm developing an online ordering system.in one part.user need to select the quatity of their order.i'm using select box.the choice limited from 1 to 10.however, even after the user select the quantity (e.g: 6 ) the value will not inserted into database.what i mean is there is no value for the quantity of order in database.is there any specific php coding to made the value inserted in database?



i'm using dreamweaver 8 and MySql.
thank you in advance
Dec 16 '09 #1
6 24074
Atli
5,058 Expert 4TB
Hey. Welcome!

How does your code look like now?

I don't know enough about your code to offer any specific help, but in general, this is how you would insert a value from a select box into a database.

Given a form like this:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <head>
  3.     <title>Example</title>
  4. </head>
  5. <body>
  6.     <form action="insert.php" method="post">
  7.         Qty: <select name="qantity">
  8.             <option value="1">1</option>
  9.             <option value="1">3</option>
  10.             <option value="1">5</option>
  11.             <option value="1">10</option>
  12.         </select>
  13.         <br>
  14.         <input type="submit">
  15.     </form>
  16. </body>
  17. </html>
The PHP code to insert the selected value into a database would look something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(isset($_POST['qty'])) {
  3.     // Fetch and clean the <select> value.
  4.     // The (int) makes sure the value is really a integer.
  5.     $qty = (int)$_POST['qty'];
  6.  
  7.     // Create the INSERT query.
  8.     $sql = "INSERT INTO `table`(`quantity`)
  9.             VALUES ({$qty})";
  10.  
  11.     // Connect to a database and execute the query.
  12.     $dbLink = mysql_connect('host', 'user', 'pwd') or die(mysql_error());
  13.               mysql_select_db('dbName', $dbLink) or die(mysql_errno());
  14.  
  15.     $result = mysql_query($sql);
  16.  
  17.     // Check the results and print the appropriate message.
  18.     if($result) {
  19.         echo "Record successfully inserted!";
  20.     }
  21.     else {
  22.         echo "Record not inserted! (". mysql_error() .")";
  23.     }
  24. }
  25. ?>
sorry in advance if my questions are stupid question.
There are no stupid questions. Even the most experienced of us had to learn the beginner stuff at some point :)
Dec 16 '09 #2
kovik
1,044 Expert 1GB
You generally don't need to check mysql_affected_rows() for INSERT queries. The query will throw an error if it does not succeed. mysql_affected_rows() is moreso for UPDATE queries.

Secondly... It's standard to use typecasting instead of intval(), unless you are using non-decimal values (i.e. hex, binary).
Dec 16 '09 #3
Atli
5,058 Expert 4TB
You generally don't need to check mysql_affected_rows() for INSERT queries. The query will throw an error if it does not succeed. mysql_affected_rows() is moreso for UPDATE queries.
True. Nice catch ;-)
That example was originally a part of a larger UPDATE example. I just edited the query and posted it here. Should have read it through more carefully xD

I'll correct the example, for the sake of future readers.
Secondly... It's standard to use typecasting instead of intval(), unless you are using non-decimal values (i.e. hex, binary).
Why do you say that? I've never really found any real benefit from using typecasting over the variable functions, other than a very minor (almost negligible) performance gain. I'd be happy to be proven wrong on this, though.

I tend to use the functions in basic examples like these because it is simpler for newbies to understand. Besides, I never really liked typecasting. Doesn't seem to fit in a loosely-typed language... but that's just me :P
Dec 16 '09 #4
kovik
1,044 Expert 1GB
One of the disadvantages of loosely-typed languages is that we have to check all of our types before using them, especially when it comes to times that a variable could be scalar or non-scalar. When we force typing via typecasting, we can always be sure of what we have.

Plus, calling a function will always be slower than using a language construct, like you said. :P
Dec 16 '09 #5
hi.
thanks a lot for you help.but i just didn't get it yet.
sorry again guys.i just didn't explain my coding problem in detail before.i have a table consist of Product Code, Product Name, Bil of Product Ordered, Order and Update. Bil of Product Ordered is where the select box is where user need to choose their quantity of product.here part of my coding where i created the table.

Expand|Select|Wrap|Line Numbers
  1. <table width="98%" border="0" bgcolor="#99CCFF" align="center" height="100">
  2. <tr bgcolor="#99CCFF" align="center">
  3. <td bgcolor="#99FFFF" align="center" height="27">
  4. <div align="center"><b>Product Code</b></div>
  5. </td>
  6. <tr bgcolor="#99CCFF" align="center">
  7. <td bgcolor="#99FFFF" align="center" height="27">
  8. <div align="center"><b>Product Name</b></div>
  9. </td>
  10. <tr bgcolor="#99CCFF" align="center">
  11. <td bgcolor="#99FFFF" align="center" height="27">
  12. <div align="center"><b>Bil of Product Ordered</b></div>
  13. </td>
  14. <tr bgcolor="#99CCFF" align="center">
  15. <td bgcolor="#99FFFF" align="center" height="27">
  16. <div align="center"><b>Order</b></div>
  17. </td>
  18. <tr bgcolor="#99CCFF" align="center">
  19. <td bgcolor="#99FFFF" align="center" height="27">
  20. <div align="center"><b>Update</b></div>
  21. </td>
  22. do {
  23. echo
  24. '<tr bgcolor="#99CCFF" align="center">
  25. <td bgcolor="#FFFFFF" align="center">' .$myrow["code_product"] . '</td>' .    '
  26. <td bgcolor="#FFFFFF" align="center">' . ucwords($myrow["product_name"]) . '</td>' .    '
  27. <td bgcolor="#FFFFFF" align="center">
  28. <select name="bil_product" size="1">
  29. <option>-</option>
  30. <option value="1">1</option>
  31. <option value="2">2</option>
  32. <option value="3">3</option>
  33. <option value="4">4</option>
  34. <option value="5">5</option>
  35. <option value="6">6</option>
  36. <option value="7">7</option>
  37. <option value="8">8</option>
  38. <option value="9">9</option>
  39. <option value="10">10</option>
  40. </select>   / Box
  41. </font></td>
  42.  
  43. <td bgcolor="#FFFFFF" align="center"><a href="addorder.php?id_product=' . $myrow["id_product"] . '"><font face="Arial, Helvetica, sans-serif" size="2"><b><font color="#000099"><input type="Radio" name="RadioButton" value="RadioButton"> </font></b></font> </div></a></td>' . '
  44.  
  45. <td bgcolor="#FFFFFF" align="center"><a href="addorder.php?id_product=' . $myrow["id_product"] . '"><font face="Arial, Helvetica, sans-serif" size="2"><b><font color="#000099">[ Order ]</font></b></font> </div></a></td>' . ' 
so.when the user click at the radio button and Order link.all the database supppose to be inserted.

here is part of my coding at addorder.php file
Expand|Select|Wrap|Line Numbers
  1. $trh = ( date("d/m/y"));
  2. echo "<br>";
  3.  $sql = "INSERT INTO orders (roc_comp,id_product,code_product,stat_order,bil_product,date_order,date_up) 
  4. VALUES ('$nokp','$idproduct','$code_product','H','$bil_product','$trh','$trh')";
  5. $result = mysql_query($sql);
  6.  
i just don't know where to put you coding. however, i will try and error while waiting for your reply.
hope you guys can understand my coding.
sorry for my poor english.
Dec 17 '09 #6
kovik
1,044 Expert 1GB
Whenever the form is submitted, perform the business logic.

To check if a form has been submitted, check if the $_POST array is empty().
Dec 17 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Coz | last post by:
Hi all, I'm looking for help again!!! I have been writing a page to update a database but now have another 'silly' problem with...
3
by: Chris Gilbert | last post by:
I'm quite stuck with this: I have an import table called ReferenceMatchingImport which contains data that has been sucked from a data submission....
1
by: garimapuri | last post by:
hi ihad an array in php and iwant to insert its value in database the coding is: <?php $nv_pairs = explode('&',$query_string); $array; list...
0
by: jtocci | last post by:
I'm having a big problem with CREATE RULE...ON INSERT...INSERT INTO...SELECT...FROM...WHERE when I want to INSERT several (20~50) records based on...
29
by: pb648174 | last post by:
I have the following basic statements being executed: Create a temp table, #TempPaging Insert Into #TempPaging (Col1, Col2) Select Col1, Col2...
10
by: Phil Latio | last post by:
I am inserting data into user table which contains 5 fields, sounds simple enough normally but 2 of the fields are designated as UNIQUE. If someone...
1
by: julianomartins | last post by:
Hi friends, how I make to recoup the value of database and to keep selected in combobox to edit my form? in script below it is selected field...
11
by: cooperkuo | last post by:
Dear all, I have a question about ADO in the subform. I know how to use ADO to insert/update/select data into the sigin form, but wehn I try to do...
1
Fary4u
by: Fary4u | last post by:
Hi is any body know where is actual problem is coz it's look me some problem ? to insert value into the MS ACCESS DATABASE after the value is...
2
chathura86
by: chathura86 | last post by:
public void insertData(String sql) { if(conn != null) { SqlCommand cmd = new SqlCommand(); cmd.Connection = conn ;...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.