Randy Gilman
Randy Gilman

Reputation: 457

Trying to insert a PHP form into SQL

Some of the code in this problem can be kind of long so I will try to pinpoint where I think the problem lies. The one issue I am having is I am not sure how to tell where the code is failing. All I have is an either it loads or it doesn't. Right now it just doesn't load without any reason why.

First I will show my form code:

function show_form($messages) {         


        // Assign post values if exist
        $username="";
        $password="";
        $firstname="";
        $lastname="";
        $address="";
        $city="";
        $state="";
        $zip="";
        $phone="";
        $card_number="";
        $expire="";
        $code="";
        $product_name="";
        if (isset($_POST["username"]))
          $username=$_POST["username"];
        if (isset($_POST["password"]))
          $password=$_POST["password"];   
        if (isset($_POST["firstname"]))
          $firstname=$_POST["firstname"];  
        if (isset($_POST["lastname"]))
          $lastname=$_POST["lastname"];
        if (isset($_POST["address"]))
          $address=$_POST["address"];     
        if (isset($_POST["city"]))
          $city=$_POST["city"];  
        if (isset($_POST["state"]))
          $state=$_POST["state"];
        if (isset($_POST["zip"]))
          $zip=$_POST["zip"];     
        if (isset($_POST["phone"]))
          $phone=$_POST["phone"];  
        if (isset($_POST["card_number"]))
          $card_number=$_POST["card_number"];
        if (isset($_POST["expire"]))
          $expire=$_POST["expire"];   
        if (isset($_POST["code"]))
          $code=$_POST["code"];  
        if (isset($_POST["product_name"]))
          $product_name=$_POST["product_name"];

    echo "<p></p>";
    echo "<h2> Enter New Customers Data</h2>";
    echo "<p></p>";     
    ?>
    <h5>Complete the information in the form below and click Submit to create your account. All fields are required.</h5>
    <form name="createstudent" method="POST" action="InsertApp.php">    

  Username:<br>
  <input type="text" name="username" value=""><br>
  Last name:<br>
  <input type="text" name="password" value=""><br>
  Street Address:<br>
  <input type="text" name="address" value=""><br>
  First name:<br>
  <input type="text" name="firstname" value=""><br>
  Last name:<br>
  <input type="text" name="lastname" value=""><br>
  Street Address:<br>
  <input type="text" name="address" value=""><br>
  City:<br>
  <input type="text" name="city" value=""><br>
  <br>
  <select name="state" size="1">
  <option value="AK">AK</option>
  <option value="AL">AL</option>
  <option value="AR">AR</option>
  <option value="AZ">AZ</option>
<option value="WV">WV</option>
  <option value="WY">WY</option>
  </select>
  <br>
  <br>
  Zipcode:<br>
  <input type="text" name="zip" value=""><br><br>
  <input type="radio" name="credit_card" value="visa" checked> Visa<br>
  <input type="radio" name="credit_card" value="master"> MasterCard<br>
  <input type="radio" name="credit_card" value="american"> American Express<br>
  <input type="radio" name="credit_card" value="discover"> Discover<br>
  <input type="radio" name="credit_card" value="paypal"> Pay Pal<br><br>
  Phone Number:<br>
  <input type="text" name="phone" valur""><br>
  Credit Card Number:<br>
  <input type="text" name="card_number" value=""><br>
  Expiration Date (Mon/Year):<br>
  <input type="text" name="expire" value=""><br>
  Security Code:<br>
  <input type="text" name="code" value=""><br><br>
  <input type="submit" value="Submit">          
    </form>

I never had a problem with the above code I am just providing it to show you what I am working with. Next I will show you some of the code that I added which gave me problems later. I know I should of just did a small bit at a time but I screwed that up.

<?php
function validate_form()
{

    $messages = array();
  $redisplay = false;
  // Assign values
  $username = $_POST["username"];
  $password = $_POST["password"];
  $firstname = $_POST["firstname"];
  $lastname = $_POST["lastname"];
  $address = $_POST["address"];
  $city = $_POST["state"];
  $state = $_POST["state"];
  $zip = $_POST["zip"];
  $phone = $_POST["phone"];
  $card_number = $_POST["card_number"];
  $expire = $_POST["expire"];
  $code = $_POST["code"];
  $product_name = $_POST["product_name"];

  $customer = new CustomerClass($username,$password,$firstname,$lastname,$address,$city,$state,$zip,$phone,$card_number,$expire,$code,$product_name);
    $count = countCostumer($customer);        


    // Check for accounts that already exist and Do insert
    if ($count==0)   
    {       
        $res = insertCustomer($customer);
        echo "<h3>Thank you for shopping with us!</h3> ";         
    }
    else 
    {
        echo "<h3>A customer account with that username already exists.</h3> ";         
    }   
  }

 function countCustomer ($customer)
  {          
    // Connect to the database
   $mysqli = connectdb();
   $firstname = $customer->getFirstname();
   $lastname = $customer->getLastname();
   $username = $->getUsername();


        // Connect to the database
    $mysqli = connectdb();

    // Define the Query
    // For Windows MYSQL String is case insensitive
     $Myquery = "SELECT count(*) as count from Customer
           where username='$username'";  

     if ($result = $mysqli->query($Myquery)) 
     {
        /* Fetch the results of the query */         
        while( $row = $result->fetch_assoc() )
        {
          $count=$row["count"];                                           
        }    

        /* Destroy the result set and free the memory used for it */
        $result->close();         
   }

    $mysqli->close();   

    return $count;

  }

  function insertCustomer ($customer)
  {

        // Connect to the database
   $mysqli = connectdb();


   $firstname = $customer->getFirstname();
   $lastname = $customer->getLastname();
   $username = $->getUsername();

        // Add Prepared Statement
        $Query = "INSERT INTO Customer 
             (username,password,firstName,lastName,address,city,state,zip,phone,card_number,expire,code,product_name) 
               VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";


        $stmt = $mysqli->prepare($Query);

$stmt->bind_param("sssssssiiiiis", $username, $password, $firstname, $lastname, $city, $state, $zip, $phone, $card_number, $expire, $code, $product_name);
$stmt->execute();



    $stmt->close();
    $mysqli->close();

        return true;
    }

I know this is a lot of code to put out there for help but I have no way of finding a way to pinpoint the error. If anyone knows of anywhere I can put this code that will display errors that may help. I tried using W3C Validation. However, it told me I could not use it on nonpublic domains.

Here is some more of the code:

class StudentClass
{
    // property declaration
    private $username="";
    private $password="";
    private $firstname="";
    private $lastname="";
    private $address="";
    private $city="";   
    private $state="";
    private $zip="";
    private $phone="";
    private $card_number="";
    private $expire="";
    private $code="";
    private $product_name="";


    // Constructor
    public function __construct($username,$password,$firstname,$lastname,$address,$city,$state,$zip,$phone,$card_number,$expire,$code,$product_name)
    {
      $this->username = $username;
      $this->password = $password;
      $this->firstname = $firstname;
      $this->lastname = $lastname;
      $this->address = $address;
      $this->city = $city;
      $this->state = $state;
      $this->zip = $zip;
      $this->phone = $phone;
      $this->card_number = $card_number;
      $this->expire = $expire;
      $this->code = $code;
      $this->product_name = $product_name;

    }

    // Get methods 
      public function getUsername ()
    {
        return $this->username;
    } 
      public function getPassword ()
    {
        return $this->password;
    } 
      public function getFirstname ()
    {
        return $this->firstname;
    } 
      public function getLastname ()
    {
        return $this->lastname;
    } 
      public function getAddress ()
    {
        return $this->address;
    } 
      public function getCity ()
    {
        return $this->city;
    } 
      public function getState ()
    {
        return $this->state;
    } 
      public function getZip ()
    {
        return $this->zip;
    } 
      public function getPhone ()
    {
        return $this->phone;
    } 
      public function getCardNumber ()
    {
        return $this->card_number;
    } 
      public function getExpire ()
    {
        return $this->expire;
    } 
      public function getCode ()
    {
        return $this->code;
    } 
      public function getProductName ()
    {
        return $this->product_name;
    } 


    // Set methods 
    public function setusername ($value)
    {
        $this->username = $value;       
    }
    public function setPassword ($value)
    {
        $this->password = $value;       
    }
    public function setFirstname ($value)
    {
        $this->firstname = $value;      
    }
    public function setLastname ($value)
    {
        $this->lastname = $value;       
    }
    public function setAddress ($value)
    {
        $this->address = $value;        
    }
    public function setCity ($value)
    {
        $this->city = $value;       
    }     
    public function setState ($value)
    {
        $this->state = $value;      
    }
    public function setZip ($value)
    {
        $this->zip = $value;        
    }
    public function setPhone ($value)
    {
        $this->phone = $value;      
    }
    public function setCardNumber ($value)
    {
        $this->card_number = $value;        
    }
    public function setExpire ($value)
    {
        $this->Expire = $value;     
    }
    public function setCode ($value)
    {
        $this->code = $value;       
    }
    public function setProductName ($value)
    {
        $this->product_name = $value;       
    }  
} // End Studentclass

?>

Any help or insight would be appreciated. Thanks

Upvotes: 0

Views: 31

Answers (1)

Iwnnay
Iwnnay

Reputation: 2008

A couple things to troubleshoot first:

If you want to turn on error_reporting, which is the easiest way to determine if you have a syntax error you can find info about that here.

In the second blurb of text you have two occasions where you seem to be missing the word customer and instead have typed $->getUsername() and in the third blurb, it may just be a copying flub, but there is not leading <?php tag.

If you're not interested in installing a IDE or Syntax checker you can always run php -l from the command line or run php_check_syntax on a file. Just some food for thought.

Upvotes: 1

Related Questions