Adding Multiple Records to MySQL Database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
csand
Forum Newbie
Posts: 6
Joined: Wed May 14, 2008 6:00 pm

Adding Multiple Records to MySQL Database

Post by csand »

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:

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">
            &nbsp;
        </td>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr> 
        <td width="100">
            &nbsp;
        </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>
 
The processing code

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';
?>
 
csand
Forum Newbie
Posts: 6
Joined: Wed May 14, 2008 6:00 pm

Re: Adding Multiple Records to MySQL Database

Post by csand »

I did hours of research last night, but I found my answer tonight at http://www.webmasterworld.com/php/3305244.htm

My new processing code is:

Code: Select all

<?php
include 'LoginDb.php';
include 'UseActives.php';
 
$newbros = $_POST['newbros'];
 
foreach($_POST['pin'] as $idx => $val) 
{ 
    $pin = $val; 
    $name = $_POST['name'][$idx]; 
    $home = $_POST['home'][$idx]; 
    $query = "insert into brothers (pin,name,home) values ('$pin','$name','$home')"; 
    mysql_query($query)
        or die('Error, insert query failed');
}
 
include 'CloseDb.php';
?>
Post Reply