【问题标题】:mysql_select_db() causes my $_POST variables to come up emptymysql_select_db() 导致我的 $_POST 变量为空
【发布时间】:2012-01-16 19:13:32
【问题描述】:

当我使用 mysql_select_db() 时,我的 $_POST 变量为空,如果我将其注释掉,那么我的 $_POST 变量很好,但我的查询可以执行。 (没有选择数据库)。我确实包含了我的连接文件。

这是我的代码的细分:

<?php 
require_once('connections/conn1.php');
include('functions.php');

print_r ($_POST);//Outputs "Array()" to the screen
if(isset($_POST['Login']))
{

    mysql_select_db($database_conn1, $conn1);
    // if this line is commented out then the print_r($_POST) 
    //above outputs all the correct information.

    $select = 'SELECT record_id, username, active FROM table1 WHERE username = "'.mysql_real_escape_string($_POST['username']).'" AND password = "'.mysql_real_escape_string(md5($_POST['password'])).'"';

    $query = mysql_query($select, $conn1) or die(mysql_error());

}
    ?>  

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table>
    <tr>
        <td align="right">Username</td>
        <td><input type="text" id="username" name="username" size="32" value="" />
      </td>
  </tr>
    <tr>
        <td align="right">Password</td>
        <td><input type="password" id="password" name="password" size="32" value="" />
      </td>
  </tr>
    <tr>
        <td align="center" colspan="2"><input type="submit" name="Login" value="Login" /></td>
    </tr>
</table>

【问题讨论】:

  • 你有mysql_connect() 设置吗?您是否尝试将 die(mysql_error()) 添加到您的 mysql_select_db() 的末尾以确保它不会死?
  • 我在 mysql_select_db() 的末尾添加了or die(mysql_error());,但没有任何改变。我的require_once('connections/conn1.php'); 包括代码我的mysql_connect() 以及主机名、数据库、用户名和密码,我在mysql_connect 的末尾还有一个or die(mysql_error());。我的 $_POST 变量为空的意思是,当我点击print_r ($_POST);//Outputs "Array()" to the screen 行时,我看到的所有输出到屏幕都是Array(),当它应该输出我所有的帖子变量的数组时。
  • 提交表单时会出现这种情况,而不是之前,在这种情况下不会调用 mysql_select_db(),因为它取决于 $_POST['login'] 索引设置,即提交表单时,因此当 $_POST 数组不为空时
  • 提交表单后,if(isset($_POST['Login'])) 这一行显示为 false,因此为了检查从我的表单传入的值是什么,我输入了 print_r ($_POST);//Outputs "Array()" to the screen 行,但它仍然显示为空提交表单后的数组。
  • 错误,您没有发布任何标记为“登录”的值。这是一个按钮,但我不是真正的表单字段-我可能是错的,但我从未遇到过安装 Move the print_r($_POST);声明到文件的最顶部并检查结果;这样,任何错误代码都无法清空您的 $_POST 变量。

标签: php database database-connection


【解决方案1】:

将 mysql_select_db($database_conn1, $conn1) 放入你的 conn1.php 文件中。

【讨论】:

  • 提交表单后,if(isset($_POST['Login'])) 这一行显示为假,因此要检查从我的表单传入的值是什么,我输入了print_r ($_POST);//Outputs "Array()" to the screen 行,但它仍然显示为空大批。我现在将尝试将mysql_select_db() 放入我的 conn1.php 文件中。
  • 将 mysql_select_db() 添加到我的 conn1.php 文件并没有改变任何东西。表单提交后,$_POST 变量仍然是一个空数组。
【解决方案2】:

我遇到了几乎相同的问题...在此处查找“通过 $_POST-value 发送数据库名称不起作用”。 我用 if (isset($_POST['dbName'])) {php-code} 包围了我的漏洞 php 代码,它可以工作:) 我花了大约 8 个小时!我希望这会是一个人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多