Adding Multiple Records to MySQL Database
Posted: Wed May 14, 2008 6:10 pm
Alright so I have a mysql database (actives) that i'm trying to add to using a html form. The html form is generated using a php script that is based on the amount of records you want to add (received from a previous form). The form generation is working fine, but html/php POST processor to work.
I have verified that it is definitely connecting to the database and brothers table- I think I am just doing the post wrong. I'm new at coding php so thanks for helping.
The form code:
The processing code
I have verified that it is definitely connecting to the database and brothers table- I think I am just doing the post wrong. I'm new at coding php so thanks for helping.
The form code:
Code: Select all
<?php
$newbros = $_GET["newbros"];
?>
<table width="400" border="0" cellspacing="1" cellpadding="2">
<form method="post" action="addBrothers.php">
<?php
$i=0;
while ($i<$newbros)
{
?>
<tr>
<td width="100">
PIN
</td>
<td>
<input name='pin[]' type="text">
</td>
</tr>
<tr>
<td width="100">
Name
</td>
<td>
<input name='name[]' type="text">
</td>
</tr>
<tr>
<td width="100">
Home
</td>
<td>
<input name='home[]' type="text">
</td>
</tr>
<?php
$i=$i+1;
}
?>
<tr>
<td width="100">
</td>
<td>
</td>
</tr>
<tr>
<td width="100">
</td>
<td>
<input name="newbros" type="hidden" value="<? echo $newbros?>">
<input name="doit" type="submit" id="doit" value="Add New Brother(s)">
</td>
</tr>
</table>
</form>
Code: Select all
<?php
include 'LoginDb.php';
include 'UseActives.php';
$newbros = $_POST['newbros'];
$i = '0';
while ($i < $newbros)
{
$pin=$_POST['pin[$i]'];
$name=$_POST['name[$i]'];
$home=$_POST['home[$i]'];
$query= "INSERT INTO brothers (pin,name,home) VALUES ('$pin','$name','$home');
mysql_query($query)
or die('Error, insert query failed');
}
echo 'Brother(s) added!';
include 'CloseDb.php';
?>