473,414 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 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 24274
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 listbox's...Grrr... I'm trying to populate the list box 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. The contents of this table have to be imported...
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 ($name, $value) = explode ('=',$nv_pairs); $i = 0;...
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 a single INSERT to a view. Either I get a 'too...
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 From SomeOtherTable Order By Col2, Col1 Select...
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 does enter a value which already exists, how do I...
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 of the table that possesss id of the first item of...
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 it in the form with subform((Datasheet). I don't...
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 true form html <input type="checkbox"...
2
chathura86
by: chathura86 | last post by:
public void insertData(String sql) { if(conn != null) { SqlCommand cmd = new SqlCommand(); cmd.Connection = conn ; //setting the connection to sql command ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML 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.