Jump to content

[SOLVED] Insert Multiple Rows From One Form


xander85

Recommended Posts

Hey All,

 

I got the general idea for my script from this thread:

http://www.webmasterworld.com/forum88/7020.htm

 

I have one form submitting a dynamic amount of identical entries depending on how many are generated from the query for the specific date. Each form "set" consists of four values, qid#, a#, a#, a#. So for example, three sets of these equal forms will create the following values:

qid1, a1, a2, a3

qid2, a4, a5, a6

qid3, a7, a8, a9

....

etc

 

I would like to insert each of these "sets" of values as a new row in my table. Right now I'm using the following code and I'm having problems:

 

$a=0;

$sql = "insert into answers (qid,a1,a2,a3) VALUES( ";

foreach ($_POST as $key => $val)

{

if ($a==0) $sql .= " ' " . $val . " ', ";

if ($a==1) $sql .= " ' " . $val . " ', ";

if ($a==2) $sql .= " ' " . $val . " ', ";

if ($a==3)

{

$sql .= "'" . $val . "'" . ")";

echo "<br>$sql<br>";

$result = mysql_query($sql);

} // endIF a = 6

if($a==3)

{

$a=0;

}

else

{

$a ++;

}

} // endFOREACH

 

However, when I do a echo of the $sql I get the following output:

 

insert into answers (qid,a1,a2,a3) VALUES( ' 1 ', ' 1 ', ' 4 ', '2')

 

insert into answers (qid,a1,a2,a3) VALUES( ' 1 ', ' 1 ', ' 4 ', '2') ' 2 ', ' 1 ', ' 5 ', '3')

 

The output of the following code is:

 

foreach ($_POST as $key => $val)

{

echo "<br>$key $val";

}

 

qid1 1

a1 1

a2 4

a3 2

qid2 2

a4 1

a5 5

a6 3

submit Submit

 

So I know the proper values are being passed.

 

The first query is fine, the second query just starts over and adds the second set to the end. It's using the proper values for the second set but not setup correctly.What am I doing wrong? I have a feeling its someone where in the loops.

Link to comment
Share on other sites

ok, im a little confused.....

 

there are three seperate forms, and each one sets a new row in a database?

if so, then simply write a foreach through the forms ( or sm=omething ) and write your data to the database.  or simply manually write each line into the file....

 

 

if its dynamic with javascript, and you want to know if a value is set?  then you just do if(isset($Vaule)) {then do stuff}

 

 

if none of those,  please post back

 

 

good luck

Link to comment
Share on other sites

Thanks for the reply.

 

Ok let me try to explain better.

 

I'm going to be displaying a set of four forms, names = qid, a1, a2, a3. I will be displaying this same set of four forms over and over. However, I want to insert each set as its OWN row in my database. I'm doing seperate rows because the amount of sets can change depending on the day.

 

I've seen examples where you just do a foreach() loop through the $_POST array but all when I try to display this array it only shows the last set of forms displayed because they have the same name. This might be a problem with my server. Example:

 

So, I changed my strategy and made it so the form names increase with each set, example:

 

Set 1: qid1, a1, a2, a3

Set 2: qid2, a4, a5, a6

.....

.....

Set N: qidN, a(N-2), a(N-1), aN

 

But what I'm having problems doing is inserting each set as its OWN row. I'm fairly new to php so I'm not sure I can write a proper foreach () loop.

 

The code I used before is:

$a=0;

$sql = "insert into answers (qid,a1,a2,a3) VALUES( ";

foreach ($_POST as $key => $val)

{

if ($a==0) $sql .= " ' " . $val . " ', ";

if ($a==1) $sql .= " ' " . $val . " ', ";

if ($a==2) $sql .= " ' " . $val . " ', ";

if ($a==3)

{

$sql .= "'" . $val . "'" . ")";

echo "

$sql

";

$result = mysql_query($sql);

} // endIF a = 6

if($a==3)

{

$a=0;

}

else

{

$a ++;

}

} // endFOREACH

 

However, this does not properly break up the sets to insert an individual one and move on. Here is the output of two INSERTS:

 

INSERT for Set 1:

insert into answers (qid,a1,a2,a3) VALUES( ' 1 ', ' 1 ', ' 4 ', '2')

 

INSERT for Set 2:

insert into answers (qid,a1,a2,a3) VALUES( ' 1 ', ' 1 ', ' 4 ', '2') ' 2 ', ' 1 ', ' 5 ', '3')

 

The first insert is perfect, those values are all the values from Set 1.

However, the second insert uses the values from the Set 1, then adds on the Set 2 values at the end. ^^^See right above^^^

 

How do I get my loop (or some other method) to do an individual insert for EACH set?

 

Link to comment
Share on other sites

It's a concatenation problem since $sql never 'resets' itself. Try defining $sql again after each query:

 

$sql = "insert into answers (qid,a1,a2,a3) VALUES( "; // query string starting value
foreach ($_POST as $key => $val)
{
if ($a==0) $sql .= " ' " . $val . " ', ";
if ($a==1) $sql .= " ' " . $val . " ', ";
if ($a==2) $sql .= " ' " . $val . " ', ";
if ($a==3)
{
$sql .= "'" . $val . "'" . ")";
echo "
$sql
";
$result = mysql_query($sql);
$sql = "insert into answers (qid,a1,a2,a3) VALUES( "; // reset query string to starting value

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.