试图将PHP表单插入SQL

时间:2016-02-25 21:13:13

标签: php sql-insert

这个问题中的一些代码可能有点长,所以我会试着找出我认为问题所在。我遇到的一个问题是我不知道如何判断代码失败的位置。我所拥有的只是它加载或不加载。现在它只是没有任何理由加载。

首先,我将展示我的表单代码:

echo

我从未遇到过上述代码的问题我只是提供它来向您展示我正在使用的内容。接下来,我将向您展示我添加的一些代码,这些代码后来给了我一些问题。我知道我应该一次做一点点但是我搞砸了。

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>

我知道这里有很多代码需要帮助,但我无法找到找出错误的方法。如果有人知道任何地方我可以把这个代码显示可能有帮助的错误。我尝试使用W3C验证。但是,它告诉我,我不能在非公共领域使用它。

以下是一些代码:

<?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;
    }

任何帮助或见解将不胜感激。感谢

1 个答案:

答案 0 :(得分:1)

首先要解决的几件事情:

如果您想启用error_reporting,这是确定您是否有语法错误的最简单方法,您可以找到有关该here的信息。

在第二篇文字中你有两次你似乎错过了customer这个词,而是输入了$->getUsername(),而在第三个模糊中,它可能只是一个复制动作,但是没有前导<?php标记。

如果您对安装IDE或语法检查程序不感兴趣,可以始终从命令行运行php -l或在文件上运行php_check_syntax。只是一些值得思考的东西。