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

Link to comment
Share on other sites

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

}

 

Link to comment
Share on other sites

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.