Jump to content

help with multi insert using for loop


violinrocker

Recommended Posts

im trying to do a multi insert form using for loop. the concept is specifying a number and then using it in the for loop. the loop then makes a number of fields depending on the specified number. after that i use another for loop to insert the grouped fields together.

 

code for specifying desired number

<?
if ($_GET[num]=="") {
?><form name="form1" method="post" action="multiup.php?num=1">
<input name="num" type="text" value="10" maxlength="2">
    <input type="submit" name="button" id="button" value="Submit">
</form><? } ?>

 

<? } else { ?><form name="form2" method="post" action="multiup2.php">
<input name="adder" type="hidden" value="">
<input name="num" type="hidden" value="<? echo $_POST[num]; ?>">


code for displaying fields
<?
for ($i=1;$i<=$_POST[num];$i++) {
echo "groupnumber<br />
<input name='eno$i' type='text' value='$i'>
Thumb
<input name='thumb$i' type='text'>
link1<br />

";}
?>

 

 

the insert code

<? if(isset($_POST['submit']) && ($_POST['submit'] == "Submit")) 

{ $con = mysql_connect("lalalalala");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("kiphi_main", $con);

for ($i=1;$i<=$_POST[num];$i++) {
$title="$eno$i";

$sql="INSERT INTO articles (title)
VALUES
('$_POST[$title]')"; }
}
if (!mysql_query($sql,$con))
  {echo testest;}?>

 

 

problem is i cant get it to work... should i try a different approach?

Link to comment
Share on other sites

You have a few errors here and there from first glance.  You don't seem to have an ending bracket for the else stated in this line:

 

<? } else { ?><form name="form2" method="post" action="multiup2.php">

 

You have one ending bracket on the second to last line of that section, but that ends the for loop in that section, not the else.

 

Also, this line:

 

{echo testest;}?>

 

I'm not really sure what that's supposed to do but the way you have it written without the variable identifier ($), "testest" would need to be a constant.  Did you mean $testest?

 

That's just a first glance.  If you are still having trouble, try to tell us what errors you are getting or maybe the incorrect output you are receiving, etc.  It's hard to debug someone else's code when you don't know what's actually wrong with it.

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.