2012-01-18 131 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>'; 
     } 
    } 
?>