Page 1 of 1

Do not insert record if empty

Posted: Mon Oct 26, 2009 12:30 pm
by dreeves
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";
}
 

Re: Do not insert record if empty

Posted: Mon Oct 26, 2009 12:35 pm
by Mirge
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";
}
 

Re: Do not insert record if empty

Posted: Mon Oct 26, 2009 1:53 pm
by dreeves
I tried that and it returned that there was a parse error on the line that contained the new If statement.

Re: Do not insert record if empty

Posted: Mon Oct 26, 2009 2:27 pm
by Mirge
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...

Re: Do not insert record if empty

Posted: Mon Oct 26, 2009 3:59 pm
by dreeves
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?

Re: Do not insert record if empty

Posted: Mon Oct 26, 2009 4:01 pm
by Mirge
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.

Re: Do not insert record if empty

Posted: Mon Oct 26, 2009 4:20 pm
by dreeves
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

Code: Select all

( ) and {}
in the code above around the "continue;". Thanks for your help. :D

Re: Do not insert record if empty

Posted: Mon Oct 26, 2009 4:22 pm
by Mirge
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

Code: Select all

( ) and {}
in the code above around the "continue;". Thanks for your help. :D
Ah :) Glad I could help. Have a good one.