【问题标题】:sending form ACTION to another php page that will process the result from 1st page将表单 ACTION 发送到另一个 php 页面,该页面将处理第一页的结果
【发布时间】:2013-09-20 00:25:18
【问题描述】:

我创建了 2 个页面。一个名为 test.php 的文件只有一些带有沼泽标准格式的 html。

<Form name ="form1" Method ="GET" Action ="result.php">

   room number: <INPUT TYPE = "TEXT" Name ="roomId">
   <INPUT TYPE = "Submit" Name = "Submit" VALUE = "Go">

</FORM>

还有另一个处理它的页面,称为 results.php。

$roomId = $_POST['roomId'];

            $sql = mysql_query("SELECT * FROM forestcourt WHERE id=$roomId");

            $id2 = 'id';
            $buildingName = 'buildingName';
            $subBuildings = 'subBuildings';
            $imagePath = 'imagePath';
            $description = 'description';


            $rows2 = mysql_fetch_assoc($sql);
            echo 'Name: ' . $rows2[$buildingName] . '<br/>' . 'Sub Buildings: ' . $rows2[$subBuildings] . '<br/>' . 'Description: ' . $rows2[$description] . '<br/>' . 'Location: ' . '<img src="../' . $rows2[$imagePath] . '"/>' . '<br/><br/>';

我希望它有效地做的是从 test.php 上的输入中获取值,并在按下提交按钮时将其存储在 results.php 页面上为$roomId。我已经将它与其他示例一起使用,但我不确定为什么它不在这里......希望我只是犯了一个简单的错误,有人可以指出它!

我知道该页面已连接到数据库,因为如果我对“results.php”进行一些评论,它会从数据库中获取信息。

如果我注释掉:

$roomId = $_POST['roomId'];

并改变:

$sql = mysql_query("SELECT * FROM forestcourt WHERE id=$roomId");

到;

$sql = mysql_query("SELECT * FROM forestcourt WHERE id=1");

results.php 页面上的信息与数据库中的信息相同。

当我完成这个过程时出现的一个错误是;

"警告:mysql_fetch_assoc():提供的参数不是有效的 MySQL 结果资源在 /websites/123reg/LinuxPackage23/ed/ge/_g/xxx.co.uk/public_html/testing/result.php 在第 49 行"

第 49 行将是:

 $rows2 = mysql_fetch_assoc($sql);

在 results.php 上。

如果有人能解释为什么它不起作用,那就太好了。 我对 MySQL 很陌生,我花了一天中最好的时间来解决这个问题!如果需要更多信息,我会尽快尝试提供。

【问题讨论】:

  • 你的表单方法是get。您应该使用$_GET['roomId'] 访问超级全局变量...或将表单方法更改为post
  • 感谢汉拿吉的回答!一天的工作终于完成了!

标签: php html mysql forms


【解决方案1】:

您应该在 test.php 中更改 method="post" 或在 results.php

中更改 $roomId = $_GET['roomId'];$roomId = $REQUEST['roomId'];

【讨论】:

  • 哇哦。就是这样。感谢您的回答!你能解释一下为什么会发生这种情况,以便我将来知道吗?谢谢。
  • 当您在表单中使用 method="GET" 时,您会使用相同的方法 ($_GET) 捕获 result.php 中的值,对于 POST 也是如此。这就是 PHP 的工作原理。您还可以在 results.php 中使用 $_REQUEST 来捕获使用 method="GET" 和 method="POST" 发送的值,但这还有其他问题 (stackoverflow.com/questions/1924939/php-request-vs-get-and-post)
  • 请@diffie 将其添加到您的答案中
【解决方案2】:
$sql = mysql_query("SELECT * FROM forestcourt WHERE id=1");

如果您希望上面的代码或$_POST['roomId'] = $roomid 之类的东西工作。

比执行以下操作:

$sql = mysql_query("SELECT * FROM forestcourt WHERE id='1'"); 

$sql = mysql_query("SELECT * FROM forestcourt WHERE id='$roomid'");

如果不是为了其他人,你可能已经解决了。

当然表单方法必须是 POST 或 GET 相应的。

【讨论】:

    【解决方案3】:

    您的操作页面是 result.php 而不是 results.php,如果您希望代码正常工作,请将您的表单更改为 POST

    <Form name ="form1" Method ="post" Action ="result.php">
    
       room number: <inpyt type = "text" Name ="roomId">
       <input TYPE = "Submit" Name = "Submit" VALUE = "Go">
    
    </FORM>
    

    在 php 中,首先检查你是否得到了这样的值:

    if(isset($_POST['roomId'])) $roomId = $_POST['roomId'];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 2021-01-30
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      相关资源
      最近更新 更多