0

I am really struggling with this issue and can't solve this one, I am trying to add a customer into a database using php but each time I click submit, it does not redirect to my view all customers page and just shows a white page and on the error log shows the following

[15-Jan-2020 23:49:25 UTC] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/itdonerightco/public_html/admin/add-customer.php:12) in /home/itdonerightco/public_html/admin/add-customer.php on line 86

[15-Jan-2020 23:49:56 UTC] PHP Warning: mysqli_stmt::execute(): (HY000/1364): Field 'user_pass' doesn't have a default value in /home/itdonerightco/public_html/admin/add-customer.php on line 69

The first error I am not too worried about as don't think that would prevent the insertion of the data to the database but think it's the second error that is preventing data being added to the database and is the one I am most confused about as I am not wanting to add a password in the database, I just want to add name, email, phone and username

The code I have is below

 <?php
   if (isset($_POST['submit']))
      {
        // get the form data

        $customer_name = htmlentities($_POST['customer_name'], ENT_QUOTES);
        $customer_email = htmlentities($_POST['customer_email'], ENT_QUOTES);
        $customer_phone = htmlentities($_POST['customer_phone'], ENT_QUOTES);
        $username = htmlentities($_POST['user_name'], ENT_QUOTES);

        // check that firstname and lastname are both not empty
        if ($customer_name == '' || $customer_phone == '' || $username == '')
        {
            // if they are empty, show an error message and display the form
            $error = 'ERROR: Please fill in all required fields!';
            renderForm($customer_name, $customer_phone, $username, $error);
        }
        else
        {

            $stmt = $mysqli->prepare("SELECT count(*) as user_exist FROM users WHERE (customer_name=? and customer_name!='') or (customer_email=? and customer_email!='') or (user_name=? and user_name!='')");

                $stmt->bind_param("sss", $customer_name, $customer_email, $username);
                //$stmt->bind_param("ssss", $customer_name, $customer_email, $customer_phone, $username);
                $stmt->execute();

                $stmt->bind_result($user_exist);
                $stmt->fetch();
                $stmt->close();

                if($user_exist){

                // show the form
                $error = 'ERROR: User already Exists!';
                renderForm( $customer_name, $customer_email, $username, $error);

                }
        else {
                    // insert the new record into the database
                    if ($stmt = $mysqli->prepare("INSERT users (customer_name, customer_email, customer_phone, user_name) VALUES (?, ?, ?, ?)"))
                    {
                        $stmt->bind_param("ssss", $customer_name, $customer_email, $customer_phone, $username);
                        $stmt->execute();
                        $stmt->close();

                    }
                    // show an error if the query has an error
                else
                    {
                        echo "ERROR: Could not prepare SQL statement.";
                    }

                    // redirect the user
            header("Location: view-all-customers.php");

        }
    }

}
    // if the form hasn't been submitted yet, show the form
    else
    {
        renderForm();
    }
// close the mysqli connection
$mysqli->close();

?>

I don't get where it's going wrong, think it's only happened since the hosting has upgraded to php version 7

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ian Haney
  • 87
  • 1
  • 2
  • 14
  • Should you be inserting into a `customers` table instead of `users`? If not, then you just need to supply a blank/null value for `user_pass` e.g. `INSERT users (customer_name, customer_email, customer_phone, user_name, user_pass) VALUES (?, ?, ?, ?, NULL)` – Nick Jan 16 '20 at 00:01
  • 2
    Your table definition requires you to provide a value for the `user_pass` column. Either change the table definition to allow a default value, or supply a value when inserting. – Barmar Jan 16 '20 at 00:09
  • I tried that way Nick and it does the same with the white page and not redirecting and the error log says the following [16-Jan-2020 00:14:39 UTC] PHP Warning: mysqli_stmt::execute(): (23000/1048): Column 'user_pass' cannot be null in /home/itdonerightco/public_html/admin/add-customer.php on line 69 – Ian Haney Jan 16 '20 at 00:16
  • Does the insert order have to be the same order as the mysql database table structure order? – Ian Haney Jan 16 '20 at 00:17
  • Not if you’re explicitly naming your columns, no. You must simply supply a value for that one column, since you have set up your database to require it. You’re being blocked by restrictions you have set up yourself. Presumably for a good reason. Either reevaluate whether those restrictions still make sense, or ensure that you fulfill them as you thought you would when you set them up. – deceze Jan 16 '20 at 08:21
  • The user has got full permissions for the database so unsure what else the issue could be – Ian Haney Jan 16 '20 at 11:38
  • Think I have just sorted it, I have set NULL in the db table for user_pass instead of None and it's now adding the info and redirecting to the view all customers page, only question I have is, if info is added does it overwrite the value NULL and store the data added from the form or will it show NULL still? – Ian Haney Jan 16 '20 at 14:46

0 Answers0