2017-09-29 76 views
-1

我有兩段代碼,似乎無法迴避哪些問題。 在這個網站搜索,我看到很多其他人有同樣的問題。使用一些給定的答案,我用我給出的代碼構建了代碼,但無濟於事。 ,其中通過所有變量(在一個PHP文件中使用「回聲」)測試我的表單 我的形式如下: 表單數據將不會進入我的數據庫

<div style="position: absolute; left: 10px; top: 290px; z-index: 6;"> 
     <form name="offerings" action="Offer_done.php" method="POST"> 
     <table> 
     <tr> 
     <td align="right">First Name:</td> 
     <td align="left"><input type="text" name="fname" required vspace="4" 
     /></td> 
     </tr> 
     <tr> 
     <td align="right">Last Name:</td> 
     <td align="left"><input type="text" name="lname" required vspace="4" 
     /></td> 
     </tr> 
     <tr> 
     <td align="right">Email:</td> 
     <td align="left"><input type="text" name="email" required vspace="4" 
     /></td> 
     </tr> 
     <tr> 
     <td align="right">Choose Your Card:</td> 
     <td><input list="card_type" name="card_type" required /></td> 
      <datalist id="card_type"> 
       <option value="American Express"> 
       <option value="Cirrus"> 
       <option value="Diners Club"> 
       <option value="Discover"> 
       <option value="MasterCard"> 
       <option value="Visa"> 
      </datalist> 
     </tr> 
     <tr> 
     <td align="right">Credit Card Num:</td> 
     <td align="left"><input type="text" name="c_number" required 
      SIZE="16" MAXLENGTH="16" vspace="4" /></td> 
     </tr> 
     <tr> 
     <td align="right">CV Code:</td> 
     <td align="left"><input type="text" name="cv_code" required SIZE="4" 
      MAXLENGTH="4" vspace="4" /></td> 
     </tr> 
     <tr> 
     <td align="right">Offering Amt($):</td> 
     <td align="left">$<input type="number" name="amount" value="1" 
      min="0" step="1.00" data-number-to-fixed="2" data-number- 
      stepfactor="100" class="currency" id="c1" name="money" required 
      SIZE="7" MAXLENGTH="7" vspace="4" /> 
     </tr> 
     <tr> 
     <td align="right"><INPUT TYPE="submit" VALUE="Submit Your Offering"> 
     </td> 

     <td><input action="action" onclick="window.history.go(-1); return 
      false;" type="button" value="Cancel - Back To Index Page" /></td> 
     </tr> 

    </table> 
    </form> 

    </div> 
    <!- - - - - - - - - - - - - - - - - - End Form- - - - - - - - - - - - - - 
    - - - - - - - -> 

我的PHP文件處理和發送到看起來像這樣:

<?php 

    $conn = mysqli_connect("localhost", "root", "xuncle", "offerings"); 
    if(!$conn) { 
     die("connection failed: " .mysqli_connect_error()); 
    } 

     $fname = $_POST['fname']; 
     $lname = $_POST['lname']; 
     $email = $_POST['email']; 
     $card_type = $_POST['card_type']; 
     $c_number = $_POST['c_number']; 
     $cv_code = $_POST['cv_code']; 
     $amount = $_POST['amount']; 




    $mysqli_query = "INSERT INTO givers (fname, lname, email, card_type, 
    c_number, cv_code, amount) 
    VALUES ($fname, $lname, $email, $card_type, $c_number, $cv_code, 
    $amount)"; 

    $result = mysqli_query($conn,$sql); 

    header("Location: index.html"); 


    ?> 

有人能讓我回到正確的軌道上嗎?

+1

的可能的複製[何時在MySQL中使用單引號,雙引號,反引號和(https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-雙引號和反引號在MySQL) – Qirel

+0

此外:https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef – Qirel

+0

**危險**:您很容易受到[SQL注入攻擊](http://bobby-tables.com/)**,您需要[防禦](http://stackoverflow.com/questions/60174/best -way-to-prevent-sql -injection-in-php)自己從。 – Quentin

回答

0

在上面的代碼中,執行查詢時正在訪問錯誤的變量。此外,您的代碼風險很高,因此您需要開始使用預準備語句,因爲您使用的api支持它。這將有助於防止SQL注入。

您的最終代碼如下:

$conn = new mysqli("localhost", "root", "xuncle", "offerings"); 
    if(!$conn) { 
     die("connection failed: " .$conn->connect_error); 
    } 

    $stmt = $conn->prepare("INSERT INTO givers (fname, lname, email, card_type, c_number, cv_code, amount) VALUES (?, ?, ?, ?, ?, ?, ?)");//prepare the statement 
    $stmt->bind_param("sssssss", $fname, $lname, $email, $card_type, $c_number, $cv_code, $amount);//bind placeholders to variables 
    if($stmt->execute() === true){//everything went fine 
    header("Location: index.html"); 
     //echo 'Data saved successfully'; 
    } else { 
     echo 'Error. Data not saved. '.$conn->error;//get error 
    } 
+1

「*一個潛在的解決方法是用單引號將你的變量包裝在查詢中*」不,只是讓它「工作」 - 直到某個變量在其中有引號。而不是告訴他們,爲什麼不展示如何使用準備好的陳述? :-) – Qirel

+0

@Qirel我已經用預先準備的語句更新了代碼。之前我沒有那樣做,因爲我通過手機輸入;) – Akintunde007

1

你在這一行$result = mysqli_query($conn,$sql);您需要將其更改爲$result = mysqli_query($conn,$mysqli_query);爲@Akintunde注意過這裏有一個錯字。我建議您使用prepared statements。這些是由數據庫服務器獨立於任何參數發送並解析的SQL語句。 檢查How can I prevent SQL injection in PHP?

<?php 
if(isset($_POST)){ 

    $conn = mysqli_connect("localhost", "root", "xuncle", "offerings"); 
    if (!$conn) { 
    die("connection failed: " . mysqli_connect_error()); 
    } 

    $result = mysqli_prepare($conn, "INSERT INTO `givers` (`fname`, `lname`, `email`, `card_type`, `c_number`, `cv_code`, `amount`) VALUES (?, ?, ?, ?, ?, ?, ?)"); 
    mysqli_stmt_bind_param($result, "ssssssd", $_POST['fname'],$_POST['lname'],$_POST['email'],$_POST['card_type'],$_POST['c_number'],$_POST['cv_code'],$_POST['amount']); 
    mysqli_stmt_execute($result); 

    header("Location: index.html"); 
} 
?>