1

i am trying to store data from with same name fields of three different types

like

<td><input name="itemname[]" value="ball" ></td>
<td><input name="itemname[]" value="racket" ></td>
<td><input name="itemname[]" value="shoes" ></td>
<td><input  name="subitem[]" value="big"></td> 
<td><input  name="subitem[]" value="grenn"></td>
<td><input  name="subitem[]" value="sports"></td>
<td><input name="quantity[]" value="10"></td>
<td><input name="quantity[]" value="10"></td>
<td><input name="quantity[]" value="10"></td>

and now i trying to insert them into the table 'selected_items' and i want somethinglike

itemname | subitem | quantity

ball------- big------10

racket----- green ----10

shoes------ sports ----10

please help me

edited

i tried doing with foreach

foreach($_POST['quantity'] as $val) {

$sql2 = "INSERT INTO items_selected (jobsheet_id,item_name,subitem,quantity) VALUES ('$jobid','aaa', 'bbb', '{$val}')";

if (!mysqli_query($this->con,$sql2))
                {
                    die('Error: ' . mysqli_error($this->con));
                }

}

the problem in the above code, i can only insert one column values. how can i insert the values for other column from post array

thank you

2
  • Can you post the PHP code you currently have to do this, and any issues etc you have with it. Commented Sep 5, 2013 at 1:37
  • hi james i have edited my question please have a look thank you Commented Sep 5, 2013 at 2:32

2 Answers 2

2

Ok i have solved it. Thanks to others who tried to help me

$quanity = count($_POST['quantity']);

$item = $_POST['itemname'];
$sub = $_POST['subitem'];
$qty = $_POST['quantity'];

for($i=0;$i<$quanity;$i++){
echo $item[$i].' '.$sub[$i].' '.$qty[$i];
// you can also use insert sql query
}
Sign up to request clarification or add additional context in comments.

Comments

1

This is very easy.... You are posting all the information as an array.

You obviously want 3 database entries to be inserted each time. (You could make life simpler by just having three different names)

So on the php retrieval page you would do the following.

<?php

CONNECT TO DATABASE

$itemname = $_POST['ball'];
$subitem = $_POST['racket'];
$quantity = $_POST['racket'];

foreach($itemname as $itemname):
foreach($subitem as $subitem):
foreach($quantity as $quantity):

SQL QUERY HERE INSERTING $itemname, $subitem, $quantity

endforeach;
endforeach;
endforeach;

Close SQL Connection

?>

The above code will shoot off nine sql queries which is the ammount of combinations you could get. You could of course check to see is any value is empty and than skip inserting. Not quite sure what the point of this, let me know if you need more help.

4 Comments

thanks for the response mate. I have tried your code. the problem is it is insert the same values in a all rows
$itemname = $_POST['itemname']; $qualifier = $_POST['qualifier']; $quantity = $_POST['quantity']; foreach($itemname as $itemname): foreach($qualifier as $qualifier): foreach($quantity as $quantity): $sql2 = "INSERT INTO items_selected (jobsheet_id,item_name,qualifier,quantity) VALUES ('$jobid','{$itemname}', '{$qualifier}', '{$quantity}')"; if (!mysqli_query($this->con,$sql2)) { die('Error: ' . mysqli_error($this->con)); } endforeach; endforeach; endforeach;
Can you confirm that you actually want 9 inserts? If you don't then specify what you what the outcome to be.. And that code should work correctly. If you like, just print_r($value); and this will show you what is going into the database
Oh ok... so you've simplified it for the question. Wow... that could get a bit messy than with so many loops. For the second lets just get it working though. I'm still waiting for the output of the print_r???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.