2012-01-18 21 views
-2

私はテキストボックスから値を取得しようとしましたが失敗しました。 これはuがしたい私のcodes.whatであることはやる、テキストボックスの外の値を取得し、フォームを投稿するとき...この単純な例で私のデータベーステキストボックスから値を取得する

<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

があなたのSQLにWHERE句を追加することに考えてみましょうか? – motto

+0

あなたのコードは無条件で実行され、フォームが送信されたかどうかにかかわらずPHPコードが実行されます。 –

答えて

2

スタートで1との値と一致していますあなたは...バック

<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>'; 
     } 
    } 
?> 
関連する問題