Switchfire Switchfire - 5 years ago 310
PHP Question

Alternative to checking if query string empty

I was just wondering if you anyone had a better way to check for an empty string, this one works but shows the error the first time you load the form, (because the querys empty).

pretty simply check here.

$order = "INSERT INTO sbh_itemsheet
(shopCode, itemNumber, itemDescription)
VALUES
('$shopCode','$itemNumber','$itemDescription')";


$result = mysql_query($order); //order executes

if($result)
{
echo("
Input data is succeed");
}
else
{
echo("
Input data is fail");
}


I can't figure out how to get rid of the first error message, really i should start the check at the beggining of the script and not the bottom, but my brains stopped working today and I thought some of you lovely people may be able to help.

Answer Source

You will need to check that the variables exist before processing your Query

$result = false;
if($shopCode !="" && $itemNumber !="" && $itemDescription !="")
{
    $order = "INSERT INTO sbh_itemsheet
    (shopCode, itemNumber, itemDescription)
    VALUES
    ('$shopCode','$itemNumber','$itemDescription')";

    $result = mysql_query($order); //order executes

    if($result)
    {
        echo("Input data is succeed");
    }
    else
    {
        echo("Input data is fail");
    }
}

My example above answers your question. I would however look into moving from mysql to mysqli or pdo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download