0

I am following a tutorial on SQL and learning how to update through PHP, everything seemed to go/work great but when I attempt to "add a customer", in this case, the data updates to the table with an ID, but the customer info that I entered in is blank.

It is as if the data is registering the new insert but not accepting any of the data that is being entered into the form. (Hopefully that makes sense.)

Below is the code for the POST in my "add_customer" page.

<?php
 if($_POST){
 //Get Variables from post array
 $first_name = mysqli_real_escape_string($_POST['first_name']);
 $last_name = mysqli_real_escape_string($_POST['last_name']);
 $email = mysqli_real_escape_string($_POST['email']);
 $password = mysqli_real_escape_string(md5($_POST['password']));
 $address = mysqli_real_escape_string($_POST['address']);
 $address2 = mysqli_real_escape_string($_POST['address2']);
 $city = mysqli_real_escape_string($_POST['city']);
 $state = mysqli_real_escape_string($_POST['state']);
 $zipcode = mysqli_real_escape_string($_POST['zipcode']); 

//Create Customer query

  $query ="INSERT INTO customers (first_name,last_name,email,password)
                      VALUES ('$first_name','$last_name','$email','$password')";

//Run query
  $mysqli->query($query);

//Create address query

  $query ="INSERT INTO customer_addresses (customer,address,address2,city,state,zipcode)
      VALUES ('$mysqli->insert_id','$address','$address2','$city','$state','$zipcode')";

//Run query
  $mysqli->query($query);

  $msg='Customer Added';
  header('Location: index.php?msg='.urlencode($msg).'');
  exit;
}

?>

I am not going to post all the code for the form, as most of it is the same minus the type and placeholder, but the gist of the form code is as follows

 <form role="form" method="post" action="add_customer.php">
          <div class="form-group">
            <label>First Name</label>
            <input name="first_name" type="text" class="form-control" placeholder="Enter First Name">
          </div>

Unfortunately I cannot add the image of the Added page(not enough rep yet), but the page will come up with a new line, all blank, but showing the edit button at the end. So something is going through the database enough to create a new id for the inputted data, but the data itself is not being stored to the database.

All help is appreciated! Please let me know if ANY additional info is needed to help clear this up.

This has been resolved. I was misunderstanding the mysqli string and did not have my connection variable.*

Leo
  • 37
  • 11
  • 2
    Recommendation... ditch that tutorial, and find one that teaches about prepared statements with bind variables – Mark Baker Feb 21 '17 at 22:35
  • 1
    did you even connect? your code is failing big time here. – Funk Forty Niner Feb 21 '17 at 22:59
  • Yea the code is all connecting and like I said I am seeing some sort of data going into the database as a new id is being created, but none of the form data is being posted to the database. – Leo Feb 21 '17 at 23:06
  • Thank you Frank for pointing out the other question! The errors were not showing up prior to changing the query from Fredster below. But once that was updated it did show that the same error I was getting was a previous question and that led to a solution! Thank you again for the help. – Leo Feb 21 '17 at 23:43

1 Answers1

0

You can use :

mysqli_query($db, ...) or die(mysqli_error($db));

instead of just

//Run query
  $mysqli->query($query);

in your queries to figure out which kind of error you're getting. It should show up after you submit the form.

Fredster
  • 776
  • 1
  • 6
  • 16
  • Perfect. Yea seeing the errors helped. That allowed me to see that the error I was getting had indeed been asked before (As fred marked it) so I was able to find that I as msyqli_real_escape needed 2 parameters, which was my connection variable. Once that was added data is now coming through! I just need to correct the edit and make sure all the correct mysqli variable is called now but I think this is set, thank you everyone for the help!! – Leo Feb 21 '17 at 23:29