Jump to content

Need help with php undefined variable error


mgmoses

Recommended Posts

I am struggling to get a php script working and desperately need some help!  The validation part of the script works perfectly when there is an error on the part of an applicant filling out the html form.  However, when the applicant fills out the html form properly, with no mistakes, the php script yields an "undefined variable error on line 41" and the script stops.  Line 41 displays as follows:  if(is_array($error) )

 

I would appreciate help as I know once I get this fixed I will then have to contend with getting the data to post to  the database!  Thanks in advance for your help!

 

The entire php script is shown below:

<?php

 

//uncomment for debugging

//print_r($_POST);

 

//most sites have magic quotes on

//but if they do not, this code simulates magic quotes

if( !get_magic_quotes_gpc() )

{

    if( is_array($_POST) )

        $_POST = array_map('addslashes', $_POST);

}

 

//make sure there is data in the name and email fields

if( empty($_POST["Name"]) )

{

    $error["Name"] = "Your Name is required.";

    $Name = "";

}

else

    $Name = $_POST["Name"];

 

if( empty($_POST["Email"]) )

{

    $error["email"] = "Please include your Email address.";

    $Email = "";

}

else

    $Email = $_POST["Email"];

 

 

 

//we should have at least 1 item ordered in the form

if( empty($_POST["Item01Qty"]) && empty($_POST["Item02Qty"]) && empty($_POST["Item03Qty"]) && empty($_POST["Item04Qty"]) && empty($_POST["Item05Qty"]) && empty($_POST["Item06Qty"]) && empty($_POST["Item07Qty"]) )

    $error["no_qty"] = "Please enter at least 1 item to order.";

 

//we need to make sure the order total was calculated before using the submit button

if( empty($_POST["Amount_To_Pay"]) )

    $error["no_qty"] = "Please go back and calculate your order total and resubmit.";

 

if(is_array($error) )

{

 

  echo "An error occurred while processing your order.";

    echo "<br>\n";

    echo "Please check the following error messages carefully, then click back in your browser and make corrections.";

    echo "<br>\n";

 

    while(list($key, $val) = each($error))

    {

        echo $val;

        echo "<br>\n";

    }

 

    //stop everything as we have errors and should not continue

    exit();

 

}

 

 

//Define the remaining variables

$Ad1 = $_POST['Ad1']; $Ad2 = $_POST['Ad2']; $City = $_POST['City']; $State = $_POST['State']; $Zip5 = $_POST['Zip5']; $Zip4 = $_POST['Zip4']; $AreaCode = $_POST['AreaCode']; $Phone3 = $_POST['Phone3']; $Phone4 = $_POST['Phone4']; $Ext = $_POST['Ext'];

$Total_Purchase = $_POST['Total_Purchase']; $NJ_Tax = $_POST['NJ_Tax']; $SH_Charge = $_POST['SH_Charge']; $Amount_To_Pay = $_POST['Amount_To_Pay'];

$Item01Qty = $_POST['Item01Qty']; $Item02Qty = $_POST['Item02Qty']; $Item03Qty = $_POST['Item03Qty']; $Item04Qty = $_POST['Item04Qty']; $Item05Qty = $_POST['Item05Qty']; $Item06Qty = $_POST['Item06Qty']; $Item07Qty = $_POST['Item07Qty'];

$Item01Total = $_POST['Item01Total']; $Item02Total = $_POST['Item02Total']; $Item03Total = $_POST['Item03Total']; $Item04Total = $_POST['Item04Total']; $Item05Total = $_POST['Item05Total']; $Item06Total = $_POST['Item06Total']; $Item07Total = $_POST['Item07Total'];

 

 

//we can store the order in a database as well

 

$link = @mysql_connect('localhost', 'Bruce', 'heatwave914');

if (!$link)

{

  echo "Could not connect: " . mysql_error();

}

else

{

    mysql_select_db('orders');

 

    $query  = "INSERT INTO orders

            ('Number', 'Name', 'Ad1', 'Ad2', 'City', 'State', 'Zip5', 'Zip4', 'Email', 'AreaCode', 'Phone3', 'Phone4', 'Ext',

                'Item01Qty', 'Item01Total', 'Item02Qty', 'Item02Total', 'Item03Qty',  'Item03Total',  'Item04Qty', 'Item04Total',  'Item05Qty',  'Item05Total',  'Item06Qty',  'Item06Total',  'Item07Qty', 'Item07Total',  'Total_Purchase',  'NJ_Tax',  'SH_Charge', 'Amount_To_Pay' )";

    $query .= " VALUES

            ('', '$Name', '$Ad1', '$Ad2', '$City', '$State', '$Zip5', '$Zip4', '$Email', '$AreaCode', '$Phone3', '$Phone4', '$Ext',

                '$Item01Qty', '$Item01Total', '$Item02Qty', '$Item02Total', '$Item03Qty', '$Item03Total', '$Item04Qty', '$Item04Total', '$Item05Qty', '$Item05Total', '$Item06Qty', '$Item06Total', '$Item07Qty', '$Item07Total', '$Total_Purchase', '$NJ_Tax', '$SH_Charge', '$Amount_To_Pay')";

 

//echo $query . "<br>\n";   

    $result = mysql_query($query);

      mysql_close($link);

}

?>

You have not checked if $error exists there. You only check if it is_array, assuming that it is there, but the only time $error is declared is inside the scope of your if statements.

 

Long story short, add at the top $error = 0 or change the if statement on line 41 to if (isset($error) && is_array($error)) {

 

Also, next time, please put your code in php tags by clicking the php button above the smilies; it's very helpful.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.