Do not insert record if empty

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

Post Reply
dreeves
Forum Commoner
Posts: 39
Joined: Thu Oct 22, 2009 8:53 am

Do not insert record if empty

Post 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";
}
 
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Do not insert record if empty

Post 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";
}
 
dreeves
Forum Commoner
Posts: 39
Joined: Thu Oct 22, 2009 8:53 am

Re: Do not insert record if empty

Post by dreeves »

I tried that and it returned that there was a parse error on the line that contained the new If statement.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Do not insert record if empty

Post 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...
dreeves
Forum Commoner
Posts: 39
Joined: Thu Oct 22, 2009 8:53 am

Re: Do not insert record if empty

Post 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?
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Do not insert record if empty

Post 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.
dreeves
Forum Commoner
Posts: 39
Joined: Thu Oct 22, 2009 8:53 am

Re: Do not insert record if empty

Post 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
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Do not insert record if empty

Post 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.
Post Reply