I've found a number of threads that deal with this issue, but I'm new to php and coding language so I don't quite understand what they're talking about or what the code means.
Here's the situation: I have a form with 4 rows and the user will enter a unique record into each row. I would like each row to be entered into the database, but if the row is left empty then I obviously do not want an empty record entered into the database.
I've thought about each input being numbered (1name, 2name, 3name, 4name; 1 address, 2address...)
But in the threads I searched I came across things like array, mysql_fetch, and isset that might be helpful. I'm looking into what they do to see if they could help.
I will post some code once I get a nudge in the right direction.
Inserting multiple records from single form
Moderator: General Moderators
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Inserting multiple records from single form
use arrays for your names within the HTML
Code: Select all
<form>
NAME 1:
<input type="text" name="name[]" /><br />
ADDRESS 1:
<input type="text" name="address[]" /><br />
<br />
NAME 2:
<input type="text" name="name[]" /><br />
ADDRESS 2:
<input type="text" name="address[]" /><br />
</form>
Re: Inserting multiple records from single form
After some reading, the array sounds like what I'm looking for. I've updated the form.html, but what changes do I need to make to the php script, which inserts the information into the database? Here is an example of the original:
Code: Select all
$Name=$_POST['Name'];
$Address=$_POST['Address'];
$query="INSERT INTO table (Name, Address) VALUES ('$Name', '$Address')";
mysql_query($query, $link) or die("Insert failed - " . mysql_errno() . ": " .
mysql_error());
Re: Inserting multiple records from single form
After researching and reading about arrays, I was able to make it work successfully using:
Code: Select all
FOREACH($_POST['Invoice_Date'] as $row=>$Invoice_Date)
{
$Invoice_Date=mysql_real_escape_string($Invoice_Date);
$ID=mysql_real_escape_string($_POST['ID'][$row]);
$Amount=mysql_real_escape_string($_POST['Amount'][$row]);
$query1="INSERT INTO invoiced_activity ($invoice_Date, .....)";
if (!mysql_query($query1, $link))
{
die("Invoiced activity insert failed - " . mysql_errno() . ": " .
mysql_error());
}
echo "$row record added";
}