无法提交输入供以后使用。 PHP

时间:2017-02-25 15:08:25

标签: php html

我正在尝试使用搜索栏来抓取用户输入,但是当我输入并提交没有任何反应时,我没有正确提交,但我不能为我的生活找到我的错误。我已经复制了以前创建的登录页面中的输入代码,该页面工作正常,因此让我更加困惑。

<div class="container">
        <div class="form-group">
    <?php
$stocksymbol = "";
    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        if (empty($_POST["stocksymbol"])) {
            $stocksymbolErr = "Please enter Username.";
            } else {
            $stocksymbol = test_input($_POST["stocksymbol"]);
            }

        function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
            }}

            $servername = "localhost";
            $username2 = "root";
            $password2 = "";
            $dbname = "mydb";

            $mysqli = new mysqli($servername, $username2, $password2, $dbname);

            if ($mysqli->connect_error) {
                die("Connection failed: " . $mysqli->connect_error);
            } 
             $query = "SELECT * FROM tblstocks WHERE Symbol = '$stocksymbol'";
             if ($result = $mysqli->query($query)) {
                    while ($row = $result->fetch_assoc()) {
                    $_SESSION['stockavailable'] = true;
                    $_SESSION['stock']= $row;
                    header('Location: item.php');   
                 }
                 $result->free();
             } 
            $mysqli->close();
    ?>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
            <label for="stocksymbol">Search:</label>
        <input type="text" class="form-control" id="stocksymbol">
        <br><br>
            <input type="submit" class="btn btn-default" name="submit" value="Submit">  
</form>

</div>  

1 个答案:

答案 0 :(得分:1)

input元素必须具有属性name,其值为您在服务器上期望的键,即stocksymbol能够接收在元素中输入的内容。

<input type="text" class="form-control" id="stocksymbol" name="stocksymbol">