2012-01-18 182 views
-2

我試圖從文本框中獲取值,但我失敗了。 這是我codes.whatü要的就是數值超出文本框,並用這個簡單的例子......當你郵寄的形式在我的數據庫從文本框中獲取值php

<form action="" method="post"> 

<strong>Code: *</strong> <input type="text" name = "code" > 
<input type="submit" name="submit" value="Submit"> 
    </form> 
    <?php 


$code= $_POST["code"]; 
$sql = "SELECT bookingref FROM starpick"; 
     $result = $mysqli->query($sql); 

     while (list($bookingref)=$result->fetch_row()) 
     { 
      if (($bookingref == $code)) 
       { 
        echo "Sorry, there was a problem uploading your file."; 

       }else 
        { 
         echo "=="; 
        } 
     } 

?> 
+3

考慮到添加WHERE子句中的SQL? – motto 2012-01-18 14:39:33

+0

您的代碼無條件運行,並執行PHP代碼是否提交表單。 – 2012-01-18 14:42:58

回答

2

開始匹配與一個值,做你會得到一個代碼回來...

<form action="" method="post"> 
    <strong>Code: *</strong> <input type="text" name="code"> 
    <input type="submit" name="submit" value="Submit"> 
</form> 
<?php 
    if (isset($_POST['code'])) { 
     $code = htmlentities($_POST['code']); 
     echo 'The code is ' . $code . '<br>'; 
    } 
?> 

一旦你知道你已經得到了你從形式會發生什麼,做到這一點...

<form action="" method="post"> 
    <strong>Code: *</strong> <input type="text" name="code"> 
    <input type="submit" name="submit" value="Submit"> 
</form> 
<?php 
    if (isset($_POST['code'])) { 
     $code = $_POST['code']; 
     $sql = "SELECT bookingref FROM starpick WHERE bookingref ='" . 
      mysql_real_escape_string($code)."';"; 
     $result = $mysqli->query($sql); 
     if (!$result) { 
      die('Invalid query: ' . mysql_error()); 
     } 
     while ($row = mysql_fetch_assoc($result)) { 
      // Do something with the result(s) 
      echo $row['bookingref'] . '<br>'; 
     } 
    } 
?>