PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
dreeves
Forum Commoner
Posts: 39 Joined: Thu Oct 22, 2009 8:53 am
Post
by dreeves » Mon Oct 26, 2009 12:30 pm
On my form, the user may enter multiple records to be inserted into a table. If the one of the records is empty, I do not want it to be inserted. I'm using arrays and a foreach loop. I had been looking at empty() to see if I could incorporate that successfully. Is there an If/else statement that I could use so an empty value is not inserted? Thanks.
Code: Select all
FOREACH($_POST['Invoice_Date'] as $row=>$Invoice_Date)
{
$Invoice_Date=mysql_real_escape_string($Invoice_Date);
$Practice_Code=mysql_real_escape_string($_POST['Practice_Code'][$row]);
$Invoiced_Amount=mysql_real_escape_string($_POST['Invoiced_Amount'][$row]);
$query1="INSERT INTO invoiced_activity (Invoice_Date, Practice_Code, Invoiced_Amount) VALUES ('$Invoice_Date', '$Practice_Code', '$Invoiced_Amount')";
if (!mysql_query($query1, $link))
{
die("Invoiced activity insert No. 1 failed - " . mysql_errno() . ": " .
mysql_error());
}
echo "$row record added";
}
Mirge
Forum Contributor
Posts: 298 Joined: Thu Sep 03, 2009 11:39 pm
Post
by Mirge » Mon Oct 26, 2009 12:35 pm
dreeves wrote: On my form, the user may enter multiple records to be inserted into a table. If the one of the records is empty, I do not want it to be inserted. I'm using arrays and a foreach loop. I had been looking at empty() to see if I could incorporate that successfully. Is there an If/else statement that I could use so an empty value is not inserted? Thanks.
Code: Select all
FOREACH($_POST['Invoice_Date'] as $row=>$Invoice_Date)
{
$Invoice_Date=mysql_real_escape_string($Invoice_Date);
$Practice_Code=mysql_real_escape_string($_POST['Practice_Code'][$row]);
$Invoiced_Amount=mysql_real_escape_string($_POST['Invoiced_Amount'][$row]);
$query1="INSERT INTO invoiced_activity (Invoice_Date, Practice_Code, Invoiced_Amount) VALUES ('$Invoice_Date', '$Practice_Code', '$Invoiced_Amount')";
if (!mysql_query($query1, $link))
{
die("Invoiced activity insert No. 1 failed - " . mysql_errno() . ": " .
mysql_error());
}
echo "$row record added";
}
Try:
Code: Select all
FOREACH($_POST['Invoice_Date'] as $row=>$Invoice_Date)
{
$Invoice_Date=mysql_real_escape_string($Invoice_Date);
$Practice_Code=mysql_real_escape_string($_POST['Practice_Code'][$row]);
$Invoiced_Amount=mysql_real_escape_string($_POST['Invoiced_Amount'][$row]);
if(empty($Invoice_Date) || empty($Practice_Code) || empty($Invoiced_Amount)) { continue; }
$query1="INSERT INTO invoiced_activity (Invoice_Date, Practice_Code, Invoiced_Amount) VALUES ('$Invoice_Date', '$Practice_Code', '$Invoiced_Amount')";
if (!mysql_query($query1, $link))
{
die("Invoiced activity insert No. 1 failed - " . mysql_errno() . ": " .
mysql_error());
}
echo "$row record added";
}
dreeves
Forum Commoner
Posts: 39 Joined: Thu Oct 22, 2009 8:53 am
Post
by dreeves » Mon Oct 26, 2009 1:53 pm
I tried that and it returned that there was a parse error on the line that contained the new If statement.
Mirge
Forum Contributor
Posts: 298 Joined: Thu Sep 03, 2009 11:39 pm
Post
by Mirge » Mon Oct 26, 2009 2:27 pm
dreeves wrote: I tried that and it returned that there was a parse error on the line that contained the new If statement.
What's the parse error? Make sure you typed it correctly...
dreeves
Forum Commoner
Posts: 39 Joined: Thu Oct 22, 2009 8:53 am
Post
by dreeves » Mon Oct 26, 2009 3:59 pm
Yes I am sure I typed it in correctly.
Here is the error I get:
Parse error: parse error in C:\wamp\www\2reimburse_insert.php on line 35
Wouldn't your suggested code just find out if the variables are empty, then continue and insert them anyway? What stops them from being inserted?
Mirge
Forum Contributor
Posts: 298 Joined: Thu Sep 03, 2009 11:39 pm
Post
by Mirge » Mon Oct 26, 2009 4:01 pm
dreeves wrote: Yes I am sure I typed it in correctly.
Here is the error I get:
Parse error: parse error in C:\wamp\www\2reimburse_insert.php on line 35
Wouldn't your suggested code just find out if the variables are empty, then continue and insert them anyway? What stops them from being inserted?
The "continue;" statement... skips the current iteration, and moves onto the next.
dreeves
Forum Commoner
Posts: 39 Joined: Thu Oct 22, 2009 8:53 am
Post
by dreeves » Mon Oct 26, 2009 4:20 pm
It works! I am so relieved. I tested it with inserting one record, then two. I think the issue was trying to tell difference between
in the code above around the "continue;". Thanks for your help.
Mirge
Forum Contributor
Posts: 298 Joined: Thu Sep 03, 2009 11:39 pm
Post
by Mirge » Mon Oct 26, 2009 4:22 pm
dreeves wrote: It works! I am so relieved. I tested it with inserting one record, then two. I think the issue was trying to tell difference between
in the code above around the "continue;". Thanks for your help.
Ah
Glad I could help. Have a good one.