Jump to content

trying to insert multiple rows into my sql from a php form


scmeeker

Recommended Posts

The information is drawn in from a table into a form that the user will click submit and all the rows will be inputted into the table with their username association.

 

With this form, its only inputting the last row and not all of them. 

 

You're help is much appreciated! :)

 

Here is the form in the PHP area:

 

while($row=mysql_fetch_array($get_items_sql)){

   

              $name  =$row['item'];

              $date = ($row['expiration_date']);

              $exp_date = date('M d Y', strtotime($row['expiration_date']));

                    $content .= "";

              $content .="<table width=\"450\" border=\"0\"><tr><td width=\"200\"><span class=\"anotherfont\"><input name=\"item[]\" type=\"hidden\" value=\"$name\" id=\"item[]\">$name</span></td><td width=\"200\"><span class=\"greenfont\"><input name=\"expiration_date[]\" type=\"hidden\" value=\"$date\" id=\"expiration_date[]\">$exp_date</span><input name=\"username[]\" type=\"hidden\" value=\"$username\" id=\"username[]\"/>

</td></tr><br /></table>";

    $content .= "\n";

            }

            $content .= "</ul>\n";

 

 

Then below is part of the HTML Form with $content referenced:

 

<form action="add_select.php?source_id=<?php echo $coupon_source ?>" method="post" enctype="multipart/form-data" name="add_new" id="add_new">

<?php echo $content ?>

 

 

<input name="SUBMIT" type="submit" value="submit" /></form><br />

 

 

Then here is the section from the "add_select.php" file from the form:

 

include('connect.php');

 

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

{

$Item_name=$item;

$expiration_date=$_POST['expiration_date'][$row];

$username=$_POST['username'][$row];

}

 

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

{

$Item_name=mysql_real_escape_string($item);

$expiration_date=mysql_real_escape_string($_POST['expiration_date'][$row]);

$username=mysql_real_escape_string($_POST['username'][$row]);

}

 

 

 

$sql2="INSERT INTO place (item, expiration_date, username) VALUES ('$Item_name', '$expiration_date', '$username')"; 

 

 

 

if (!mysql_query($sql2))

 

  {

 

  die('Error: ' . mysql_error());

 

  }else {

     

     

    header("location:insert_complete.php");

}

                 

Link to comment
Share on other sites

This post is going to assume that you are receiving the data that you expect.

 

Try running this in your add_select.php


include('connect.php');

foreach($_POST['item'] as $row=>$item)
{
$Item_name=mysql_real_escape_string($item);
$expiration_date=mysql_real_escape_string($_POST['expiration_date'][$row]);
$username=mysql_real_escape_string($_POST['username'][$row]);

$query_row[] = "('$item_name','$expiration_date','$username')";
}





$sql2="INSERT INTO place (item, expiration_date, username) VALUES " . implode(',',$query_row); 



if (!mysql_query($sql2))

  {

  die('Error: ' . mysql_error());

  }else {
     
     
    header("location:insert_complete.php");
}

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.