【问题标题】:How to use the form value in php function?如何在php函数中使用表单值?
【发布时间】:2012-09-09 10:31:38
【问题描述】:

我是 php 的新手。我使用 php 编写了自动完成文本框,并且我有一个提交按钮。我没有给出表单操作。

这是我用于自动完成文本框的 HTML 表单代码。这个自动完成文本框选择值

<form  method="post" autocomplete="off">
    <p>
        <b>Theater Name</b> <label>:</label>
        <input type="text" name="theater" id="theater" />
    </p>
    <input type="submit" value="Submit" />
</form>

我有另一个 php 函数,它根据 where 子句检索值。在 where 语句中,我想使用表单中的选定值。

例如:从剧院名称 ="form value" 的影院中选择地址

如何在php函数中使用表单值?谁能帮帮我?

 <?php
$con = mysql_connect("localhost","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("theaterdb", $con);

$result = mysql_query("SELECT * FROM theter
WHERE theater_name="<!-- This could be value that we get after clicking submit button-->);

while($row = mysql_fetch_array($result))
  {
  echo $row['thearer_name'];
  echo "<br />";
  }
?> 

提前致谢......

【问题讨论】:

  • 不直接相关,但鉴于您是 PHP 新手,您最好使用已开始弃用过程的函数,例如 @ 987654323@。请改用mysqli_PDO。两者都可以让您更好地处理可能的注入

标签: php mysql html


【解决方案1】:

您可以通过$_POST['theater']$_POST 获取值。

注意,不要直接在sql中使用这个值,需要将其转义以防止sql注入。

$theater = mysql_escape_string($_POST['theater']);
$result = mysql_query("SELECT * FROM theter WHERE theater_name='$theater'";

最后,你可以看看PDO,它是在旧的mysql_* 函数上推荐的。

【讨论】:

    【解决方案2】:
    $_POST["your_variable_name"] // for POST
    $_GET["your_variable_name"] // for GET
    

    如需深入了解,请前往:http://www.php.net/manual/en/language.variables.external.php

    【讨论】:

      【解决方案3】:

      首先,尝试使用 mysqli 而不是 mysql (mysqli_query, mysqli_connect)。使用它有许多安全/速度优势,并且具有几乎完全相同的功能。

      虽然上述答案提到使用 $_POST['theater'](您输入的名称),但请务必在将其放入查询之前转义您的帖子。

      $con = mysqli_connect("localhost","root", "YOUR PASSWORD HERE", "YOUR DATABASE HERE");
      if (!$con)
        {
        die('Could not connect: ' . mysqli_error());
        }
      
       // No need for this, please see the updated mysqli_connect as the 4th parameter selects your DB
       //mysqli_select_db("theaterdb", $con);
      
      // Please notice the last parameter of the mysqli_real_escape_string is your Input's POST
      $query = "SELECT * FROM theater WHERE theater_name=".mysqli_real_escape_string($con, $_POST['theater']);
      
      $result = mysqli_query($con, $query);
      
      while($row = mysqli_fetch_array($result))
        {
        echo $row['thearer_name'];
        echo "<br />";
        }
      

      【讨论】:

        【解决方案4】:

        首先,将您的提交按钮代码更改为以下内容:

        <input name="submit" type="submit" value="Submit" />
        

        现在,这是您应该用于查询的代码:

        <?php
        if (isset($_POST['submit'])) {
            $con = mysql_connect("localhost","root");
            if (!$con)
            {
                die('Could not connect: ' . mysql_error());
            }
        
            mysql_select_db("theaterdb", $con);
        
            $result = mysql_query("SELECT * FROM theater
            WHERE theater_name='" . mysql_real_escape_string($_POST['theater']) . "'");
        
            while($row = mysql_fetch_array($result))
            {
                echo $row['theater_name'];
                echo "<br />";
            }        
        }
        

        首先,我检查用户是否提交了表单。然后,我将他提交的数据转义并插入到您的查询中。

        * 注意:我写的所有内容都是基于在提交表单后执行代码的假设。

        * 另一个注意事项:您应该阅读有关使用 PDO 而不是 MYSQL 函数的信息。

        【讨论】:

          猜你喜欢
          • 2020-11-15
          • 2021-07-05
          • 1970-01-01
          • 1970-01-01
          • 2011-03-11
          • 2020-03-24
          • 2018-11-04
          • 2014-08-03
          • 1970-01-01
          相关资源
          最近更新 更多