Nokelekele
Nokelekele

Reputation: 55

PHP check if not empty and the value of another GET

I have a POS and need to check 2 things before I save a sale in MySQL

Code to get and check the information I need

$invoice=mysqli_real_escape_string($db,$_POST['same_invoice']);
$customer=mysqli_real_escape_string($db,$_POST['same_customer']);

If invoice is empty the customer is also empty because is the first prodyct in this invoice so we assign a invoice number and we ask the customer to show identification, the invoice is generated automatic so we input the ID-number of the customer and save to MySQL

Now the problem

If the customer wants to buy 2 products we dont need to give the order a new invoice number neither the ID but we must check both in MySQL

code I am trying but not working

if( $invoice is empty && $customer is also empty)
{ we create new invoice an ask customer for id first time }
if( $invoice is empty && $customer is NOT empty)
{ we create new invoice but do not ask customer for id }
else
{ we use the $invoice and $customer comming from the form }

Any idea how to check that?

Upvotes: 0

Views: 125

Answers (2)

Hassan ALi
Hassan ALi

Reputation: 1331

Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.

 $invoice=mysqli_real_escape_string($db,$_POST['same_invoice']);
    $customer=mysqli_real_escape_string($db,$_POST['same_customer']);
    if(!empty($invoice)&&!empty($invoice)){
    //do whatever you want to do with it.
}

No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false.

Upvotes: 0

Zac Brown
Zac Brown

Reputation: 436

You should check that the $_POST values are actually set, first. I would guess that is where your error is coming from.

$invoice = (isset($POST['same_invoice']) ? $_POST['same_invoice'] : null;
$customer = (isset($POST['same_customer']) ? $_POST['customer'] : null;

if(empty($customer) || empty($invoice)) {
  //Use values from form
} else {
  //Create new invoice
}

Upvotes: 1

Related Questions