0

Hey i need assistance with this error i am having **Notice: Undefined variable: feedback in C:\xampp\htdocs\ZoomiezWebApp\addNewCustomer.php on line 16** . I am trying to create a form to add a new customer.

This is the code

 <?php
  #1> Retrieve Form Details
  $fname= $_POST['fname'];
  $lname= $_POST['lname'];
  $address= $_POST['address'];
  $cNumber= $_POST['contactNumber'];


  #2 SANITIZE AND VALIDATE DATA
  $fname = filter_var($fname, FILTER_SANITIZE_STRING);
  $lname = filter_var($lname, FILTER_SANITIZE_STRING);
  $address = filter_var($address, FILTER_SANITIZE_STRING);
  $cNumber = filter_var($cNumber, FILTER_SANITIZE_STRING);

if($fname==""){//Validate fields for null or empty
    ***ERROR IS IN THIS LINE***$feedback .= "<br>First Name Field Empty.";
}if($lname==""){
    $feedback .= "<br>Last Name Field Empty.";
}if($address==""){
    $feedback .= "<br>Address Field Empty.";
}if($cNumber==""){
    $feedback .= "<br>Contact Number Field Empty.";
}else{ //Validation Passed...


    // #3> CONNECT MYSQL ON THE DB SERVER /
    $con = mysql_connect('localhost', "root", "test")
    or die ('Could not connect to the database');

    // #4> SELECT THE DATABASE ON THE SERVER /
    $con = mysql_select_db('zoomiezdb', $con)
    or die ('Could not locate database');

    // #5> CREATE INSERT QUERY
    $addCustomerQuery = 
        "INSERT INTO customertable (First Name, Last Name, Address, Contact Number) VALUES ('$fname', '$lname', '$address', '$cNumber'))                                                                     ";

    // #6> EXECUTE QUERY
    $queryResult = mysql_query($addCustomerQuery);

    // #7> VERIFY IF QUERY IS SUCCESSFUL
    if($queryResult)    
        $feedback = "New Customer Added";
    else
        $feedback = "<i>Add New Customer was Unsuccessful.</i>
                <br>    
                Please Contact Site Administrator...";  


    // #8> REDIRECT
 Header("Location:newCustomer.php?feedbackMsg=$feedback");

}

?>

alana
  • 1
  • 2

1 Answers1

0

You should initialize that variable before the conditions if there is no error $feedback never initializes in this case you can initialize it with '' because when you concatenate it PHP doesn't have anything to concatenate

dnetix
  • 320
  • 3
  • 10
  • okay i did. but when i click the submit button (all fields are empty) the page is going blank. Additionally when i fill out details in the form. The error message i set is being shown **(Add New Customer was Unsuccessful. Please Contact Site Administrator...)** – alana Jul 19 '14 at 03:30
  • Do a var_dump to the variables after the sanitize and check the value that it's assigned to it and then you can do the conditions correctly. But you can validate that also before posting it with JavaScript or / and html5 with the required tag – dnetix Jul 19 '14 at 04:19