Jump to content

foreach insert multiple array


dwex

Recommended Posts

 

I have six rows of text boxes for users to enter into. The fields are named like

 

ROW1: invqty, invpart, invdesc, invlist, invprice, parttotal

ROW2: invqty02, invpart02, invdesc02, invlist02, invprice02, parttotal02

ROW3: ...

ROW4: ...

 

How can I add these to variables so $invoiceqty = invqty, invqty2, invqty3, etc. $invoicepart = invpart, invpart02, invpart03, etc.

 

and then loop through them, inserting all rows into a table until all data is INSERTED

Link to comment
https://forums.phpfreaks.com/topic/225275-foreach-insert-multiple-array/
Share on other sites

don't really have the codes for these at the moment but a rough example would be like this..

 

I will like to send all 3 users into the database with their information using a foreach loop

 

<form method="POST" action="input.php">

<!-- Person #1 -->
<input type="text" name="username[]" />
<input type="text" name="phonenum[]" />
<!-- Person #2 -->
<input type="text" name="username[]" />
<input type="text" name="phonenum[]" />
<!-- Person #3 -->
<input type="text" name="username[]" />
<input type="text" name="phonenum[]" />

<input type="submit" />
</form>

Okay , I managed to bring in all informations into the database except the image?

 

This is how my foreach loop looks like.

 

foreach ($_POST['name'] as $row=>$name)

{

$item = $name;
$type = $_POST['tabledrop'][$row];
$price = $_POST['price'][$row];
$des = $_POST['description'][$row];
$stk = $_POST['stock'][$row];
$file = $_POST['pbimage']['name'][$row];

$target_path = "C:/xampp/htdocs/paintball/itemimages/";
			$target_path = $target_path . basename( $_FILES['pbimage[]']['name[]']); 

			if(move_uploaded_file($_FILES['pbimage[]']['tmp_name[]'], $target_path)) {
   				 echo "The file ".  basename( $_FILES['pbimage[]']['name[]']). 
    			" has been uploaded/";

			}


$query = "INSERT into $type SET {$type}_name = '$item' , {$type}_price = '$price' , {$type}_image = '$file', {$type}_description = '$des' , {$type}_stock = '$stk' , {$type}_time = NOW()" ;

$result = mysqli_query($link, $query) or die(mysqli_error($link));

}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.