Jump to content

Recommended Posts

This is my order.php file. Now I try to make a new php-file for processing the ordered items on this page, into another table "ordered_products" in the database. I want to INPUT following values into the table "ordered_products":

'order_num', 'order_item' and 'order_ant'

 

I know how to insert rows in a table using the INSERT query, but I don't know how to do it while looping the arrays. I have to fetch the arrays from this page, and insert them row by row in the "ordered_products" table. Can you help me with some code, please?

 

 


<form action="postorder.php" method="post">


<?

$user="*****";
$password="*****";
$database="*****";
$host="*****";

$kund_id=$_POST['kund_id'];
$prod_id=$_POST['prod_id'];
$prod_nr_ant=$_POST['prod_nr_ant'];
$prod_namn=$_POST['prod_namn'];
$prod_pris=$_POST['prod_pris'];

mysql_connect($host,$user,$password);
@mysql_select_db($database) or die("Unable to select database");
$query= "SELECT * FROM table WHERE kund_id='$kund_id'
ORDER BY order_num DESC LIMIT 1";

$result=mysql_query($query);
$num=mysql_num_rows($result);

mysql_close();


$i=0;
while ($i < $num) {

$order_num=mysql_result($result,$i,"order_num");
$kund_id=mysql_result($result,$i,"kund_id");



echo "<input type=\"text\" size=\"2\"  name=\"order_num\" value=\"$order_num\">";
echo "<input type=\"text\" name=\"kund_id\" size=\"3\" value=\"$kund_id\">";
echo "<br>";


echo "</br><br>";


$i++;
}



?>

<?php
   foreach ($_POST['prod_namn'] as $key => $val) {
       if ($_POST['prod_nr_ant'][$key] != 0) {
           echo '<input type="text" value="' . $key . '" name="order_item" size="5">';
echo '<input type="text" value="' . $val . '" size="50">';
echo '<input type="text" value="' . $_POST['prod_nr_ant'][$key] . '" name="order_ant" size="5"><br>'; 

       }
    }
?>

<?php
   foreach ($_POST['kat_namn'] as $key2 => $val2) {
       if ($_POST['kat_ant'][$key2] != 0) {
           echo '<input type="text" value="' . $key2 . '" name="order_item" size="5">'; 
echo '<input type="text" value="' . $val2 . '" size="50">';
echo '<input type="text" value="' . $_POST['kat_ant'][$key2] . '" name="order_ant" size="5"><br>'; 


       }
    }
?>




<input type="submit" value="Submit">


</form>

foreach ($insertCols as $col => $val) {
    $cols .= "`" . $col . "`";
    $vals .= "'" . $val . "',";
}
$cols = substr($cols, , -1);
$vals = substr($vals, , -1);

$sql = "INSERT INTO table_name (" . $cols . ") VALUES(" . $vals . ");";

 

Something like that might help.

Hi. Sorry that I didn't explain well enough. This is the query I try to run on the postorder.php page:

(I only get this Warning: Invalid argument supplied for foreach() in /home/virtual/etralius.se/public_html/order/postorder.php on line 13) ???

 



<?

$user="*****";
$password="*****";
$database="*****";
$host="*****";



   foreach ($_POST['order_item'] as $key => $val) {
       if ($_POST['order_ant'][$key] != 0) {
           echo 'Order num: ' . $key . ', Order Item: ' . $val . ', Quantity: ' . $_POST['order_ant'][$key] . '<br>'; 
//

mysql_connect($host,$user,$password);
@mysql_select_db($database) or die("Unable to connect to database");
$query= "INSERT INTO ordered_products VALUES ('$order_num','$order_item','$order_ant')";
mysql_query($query);
mysql_close();

//
       }
    }

?>

 

I have a problem looping the query through the ordered items, inserting them row by row in the database table "ordered_items". Every row will have the same "order_num" wich is the number of the order. "Order_item" is the product number for the product ordered. "Order_ant" is the quantity ordered for the product.

 

Example: A customer has ordered 5 rolls of Thermal paper (product#009), and 10 rolls of printerpaper (product#018). At the order.php page, the customer will be able to see what he has ordered, and then confirm his order by pushig the submit-button. The data is then transmitted to the postorder.php page, where my script is supposed to INSERT the order into the "ordered_products" table:

 

order_numorder_itemorder_ant

3280095

32801810

 

I have to use arrays doing this, since I have about 150 different items in the store.

 

Can you give me some ideas on what to change in the script to make it work?

 

Thank you, and lots of kisses from a "dumb blond" scandinavian  :-*

 

 

 

at the top of your page, put this line of code in:

echo "<pre>". print_r($_POST, true) ."</pre>\n";

 

this will print everything that was posted by your form. you will use this information to construct a valid foreach loop and insert query. once you do that, post what the results of your $_POST array are in this thread and we'll go from there.

Something like

 

<?php
if (isset($_POST['order_num'])) {
    $order_num = $_POST['order_num'];
    foreach ($order_prod as $k => $prod) {
        $qty = $_POST['order_ant'][$key];
        if ($qty > 0) {
            $sql = "INSERT INTO orderitems (order_num, order_prod, order_ant) VALUES ('$order_num', '$prod', $qty)";
            mysql_query($sql) or die (mysql_error()."<p>$sql</p>");
        }
    }
}
?>
<form method='post'>
Order Number  <input type='text' name='order_num' size='6'><br>
Product <input type='text' name='order_prod[]' size='6'> Qty <input type='text' name='order_ant[]' size='6'> <br>
Product <input type='text' name='order_prod[]' size='6'> Qty <input type='text' name='order_ant[]' size='6'> <br>
Product <input type='text' name='order_prod[]' size='6'> Qty <input type='text' name='order_ant[]' size='6'> <br>
Product <input type='text' name='order_prod[]' size='6'> Qty <input type='text' name='order_ant[]' size='6'> <br>
Product <input type='text' name='order_prod[]' size='6'> Qty <input type='text' name='order_ant[]' size='6'> <br>
<input type='submit' name='action' value='Submit'> 
<form>

  • 2 weeks later...

Hi, Barand.

 

Thank you. I think it's almost complete. It's just that the script you gave me give this Warning:

"Invalid argument supplied for foreach() in /home/virtual///postorder.php on line 24"

 

Line 24 is this line:

 

foreach ($order_item as $k => $prod) {

 

Do you know what types of problems that can cause this? I use PHP version 4.4.4, so I don't think the version is too old.

 

:) Naima

 

 

 

Hi.

 

I have struggled a lot with this issue. Now it seems that what I get from the ordr.php page, is several strings, not arrays. So, can anyone help me looping strings to insert them into my MySql database table?

 

Thank you :)

 

This is my order.php file. Now I try to make a new php-file for processing the ordered items on this page, into another table "ordered_products" in the database. I want to INPUT following values into the table "ordered_products":

'order_num', 'order_item' and 'order_ant'

 

I know how to insert rows in a table using the INSERT query, but I don't know how to do it while looping the arrays. I have to fetch the arrays from this page, and insert them row by row in the "ordered_products" table. Can you help me with some code, please?

 

 


<form action="postorder.php" method="post">


<?

$user="*****";
$password="*****";
$database="*****";
$host="*****";

$kund_id=$_POST['kund_id'];
$prod_id=$_POST['prod_id'];
$prod_nr_ant=$_POST['prod_nr_ant'];
$prod_namn=$_POST['prod_namn'];
$prod_pris=$_POST['prod_pris'];

mysql_connect($host,$user,$password);
@mysql_select_db($database) or die("Unable to select database");
$query= "SELECT * FROM table WHERE kund_id='$kund_id'
ORDER BY order_num DESC LIMIT 1";

$result=mysql_query($query);
$num=mysql_num_rows($result);

mysql_close();


$i=0;
while ($i < $num) {

$order_num=mysql_result($result,$i,"order_num");
$kund_id=mysql_result($result,$i,"kund_id");



echo "<input type=\"text\" size=\"2\"  name=\"order_num\" value=\"$order_num\">";
echo "<input type=\"text\" name=\"kund_id\" size=\"3\" value=\"$kund_id\">";
echo "<br>";


echo "</br><br>";


$i++;
}



?>

<?php
   foreach ($_POST['prod_namn'] as $key => $val) {
       if ($_POST['prod_nr_ant'][$key] != 0) {
           echo '<input type="text" value="' . $key . '" name="order_item" size="5">';
echo '<input type="text" value="' . $val . '" size="50">';
echo '<input type="text" value="' . $_POST['prod_nr_ant'][$key] . '" name="order_ant" size="5"><br>'; 

       }
    }
?>

<?php
   foreach ($_POST['kat_namn'] as $key2 => $val2) {
       if ($_POST['kat_ant'][$key2] != 0) {
           echo '<input type="text" value="' . $key2 . '" name="order_item" size="5">'; 
echo '<input type="text" value="' . $val2 . '" size="50">';
echo '<input type="text" value="' . $_POST['kat_ant'][$key2] . '" name="order_ant" size="5"><br>'; 


       }
    }
?>




<input type="submit" value="Submit">


</form>

If you look at the code I posted, you'll see that the text input fields in the form have names ending with "[]".

 

This causes them to be posted as an arrays of values which are then processed by the foreach loop.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.