how to insert value from select box into database? – PHP

  php

Q(Question):

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

A(Answer):

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:

<!DOCTYPE html>
<head>
<title>Example</title>
</head>
<body>
<form action="insert.php" method="post">
Qty: <select name="qantity">
<option value="1">1</option>
<option value="1">3</option>
<option value="1">5</option>
<option value="1">10</option>
</select>
<br>
<input type="submit">
</form>
</body>
</html>

The PHP code to insert the selected value into a database would look something like this:

<?php
if(isset($_POST['qty'])) {
// Fetch and clean the <select> value.
// The (int) makes sure the value is really a integer.
$qty = (int)$_POST['qty'];
// Create the INSERT query.
$sql = "INSERT INTO `table`(`quantity`)
VALUES ({$qty})";
// Connect to a database and execute the query.
$dbLink = mysql_connect('host', 'user', 'pwd') or die(mysql_error());
mysql_select_db('dbName', $dbLink) or die(mysql_errno());
$result = mysql_query($sql);
// Check the results and print the appropriate message.
if($result) {
echo "Record successfully inserted!";
}
else {
echo "Record not inserted! (". mysql_error() .")";
}
}
?>

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 🙂

A(Answer):

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).

A(Answer):

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 😛

A(Answer):

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. 😛

A(Answer):

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.


<table width="98%" border="0" bgcolor="#99CCFF" align="center" height="100">
<tr bgcolor="#99CCFF" align="center">
<td bgcolor="#99FFFF" align="center" height="27">
<div align="center"><b>Product Code</b></div>
</td>
<tr bgcolor="#99CCFF" align="center">
<td bgcolor="#99FFFF" align="center" height="27">
<div align="center"><b>Product Name</b></div>
</td>
<tr bgcolor="#99CCFF" align="center">
<td bgcolor="#99FFFF" align="center" height="27">
<div align="center"><b>Bil of Product Ordered</b></div>
</td>
<tr bgcolor="#99CCFF" align="center">
<td bgcolor="#99FFFF" align="center" height="27">
<div align="center"><b>Order</b></div>
</td>
<tr bgcolor="#99CCFF" align="center">
<td bgcolor="#99FFFF" align="center" height="27">
<div align="center"><b>Update</b></div>
</td>
do {
echo
'<tr bgcolor="#99CCFF" align="center">
<td bgcolor="#FFFFFF" align="center">' .$myrow["code_product"] . '</td>' . '
<td bgcolor="#FFFFFF" align="center">' . ucwords($myrow["product_name"]) . '</td>' . '
<td bgcolor="#FFFFFF" align="center">
<select name="bil_product" size="1">
<option>-</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select> / Box
</font></td>
<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>' . '
<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


$trh = ( date("d/m/y"));
echo "<br>";
$sql = "INSERT INTO orders (roc_comp,id_product,code_product,stat_order,bil_product,date_order,date_up)
VALUES ('$nokp','$idproduct','$code_product','H','$bil_product','$trh','$trh')";
$result = mysql_query($sql);

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.

A(Answer):

Whenever the form is submitted, perform the business logic.

To check if a form has been submitted, check if the $_POST array is empty().

LEAVE A COMMENT