Jump to content

Insert Multiple Rows Into A Mysql Table Using Php Array


gilbertsavier

Recommended Posts

Hi,I have a form that asks the applicant for previous involvement. They have the option of entering up to three rows and after they press submit the computer needs to enter possible multiple rows. Here is my form code:<form action="insert1.php" method="post"><table width="77%"><td height="63" colspan="5"><h3>Other involvement during high school, college (clubs, sports, work, volunteer, etc.): </h3></td></tr><tr><td width="20%"><h3>Activity</h3></td><td width="19%"><h3>Position</h3></td><td width="23%"><h3>Start Date</h3></td><td width="25%" height="60"><h3>End Date</h3></td></tr><tr><td height="63"><input name="Activity[]" type="text" id="Activity[]" size="15" /><td height="63"><input name="Position[]" type="text" id="Position[]" size="15" /><td height="63"><input name="StartDate[]" type="text" id="StartDate[]" size="15" /><td height="63"><input name="EndDate[]" type="text" id="EndDate[]" size="15" /></tr><tr><td height="63"><input name="Activity[]" type="text" id="Activity[]" size="15" /><td height="63"><input name="Position[]" type="text" id="Position[]" size="15" /><td height="63"><input name="StartDate[]" type="text" id="StartDate[]" size="15" /><td height="63"><input name="EndDate[]" type="text" id="EndDate[]" size="15" /></tr><tr><td height="63"><input name="Activity[]" type="text" id="Activity[]" size="15" /><td height="63"><input name="Position[]" type="text" id="Position[]" size="15" /><td height="63"><input name="StartDate[]" type="text" id="StartDate[]" size="15" /><td height="63"><input name="EndDate[]" type="text" id="EndDate[]" size="15" /></table><p> </p><p><input type="submit" name="Submit" id="Submit" value="Submit" /></p></form>Here is the insert1.php file<?php$con = mysql_connect("localhost","Application","*******");if (!$con){die('Could not connect: ' . mysql_error());}mysql_select_db("CpaApp", $con);//Assign each array to a variableforeach($_POST['Activity'] as $row=>$Act){$Activity=$Act;$Position=$_POST['Position'][$row];$StartDate=$_POST['StartDate'][$row];$EndDate=$_POST['EndDate'][$row];}//enter rows into databaseforeach($_POST['Activity'] as $row=>$Act){$Activity=mysql_real_escape_string($Act);$Position=mysql_real_escape_string($_POST['Position'][$row]);$StartDate=mysql_real_escape_string($_POST['StartDate'][$row]);$EndDate=mysql_real_escape_string($_POST['EndDate'][$row]);}$involv = "INSERT INTO Involvement (Activity, Position, StartDate, EndDate)VALUES ('.$Activity.','.$Position.','.$StartDate.','.$EndDate.')";if (!mysql_query($involv,$con)){die('Error: ' . mysql_error());}echo "$row record added";mysql_close($con)?>

Link to comment
Share on other sites

You should only have one loop, the first loop isn't doing anything, it's just assigning everything to variables, which you end up overwriting anyway with the next loop. You only need the second loop, and you need to send an insert query for each set of variables, so move the query code into the loop also.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...