【问题标题】:get the value out of a textbox php [closed]从文本框php中获取值[关闭]
【发布时间】:2012-01-18 14:37:43
【问题描述】:

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

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

?>

【问题讨论】:

  • 考虑在你的 sql 中添加 WHERE 子句?
  • 您的代码无条件运行,无论表单是否提交,都执行 PHP 代码。

标签: php textbox assign


【解决方案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>';
        }
    }
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 2012-02-12
    • 2015-12-21
    • 1970-01-01
    相关资源
    最近更新 更多