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
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: - <!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 :)
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).
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
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
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.
Whenever the form is submitted, perform the business logic.
To check if a form has been submitted, check if the $_POST array is empty().
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: chathura86 |
last post by:
public void insertData(String sql)
{
if(conn != null)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn ;...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
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.
...
|
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...
| |