从文本框中获取值

时间:2012-01-18 14:37:44

标签: php textbox assign

我尝试从文本框中获取值,但我失败了。 这是我的代码。你想要的是从文本框中获取值并将值与我的数据库中的值匹配

<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 "==";
                    }
        }

?>

1 个答案:

答案 0 :(得分: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>';
        }
    }
?>