![]() |
| Insert Multiple Rows Into A Mysql Table Using Php Array |
|
gilbertsavier
|
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 variable foreach($_POST['Activity'] as $row=>$Act) { $Activity=$Act; $Position=$_POST['Position'][$row]; $StartDate=$_POST['StartDate'][$row]; $EndDate=$_POST['EndDate'][$row]; } //enter rows into database foreach($_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) ?> |
||||||||||||
|
|
|||||||||||||
|
nathacof
Forum Admin
|
To insert multiple rows with one query see Mysql.com's INSERT SYNTAX documentation.
You can generate the query like so:
Make sure you properly validate and filter your untrusted inputs!!! |
||||||||||||||||
|
|
|||||||||||||||||
| Insert Multiple Rows Into A Mysql Table Using Php Array |
|
||
|


