比较提交与数据库的价值

发布于 2024-11-27 08:57:00 字数 1467 浏览 0 评论 0原文

“dbcred.php”类

<?php
   # pdo_testdb_connect.php - function for connecting to the "test" database

   function testdb_connect ()
   {
     $dbh = new PDO("mysql:host=localhost;dbname=dbname", "root", "");
     return ($dbh);
   }
?>

PHP

<?php
#connect mysql
require_once "dbcred.php";
$dbh = testdb_connect ();

session_start(); 
$username = $_POST['regduser']; 
$userpass = md5($_POST['regdpass']); 
$result = mysql_query("SELECT * FROM Student WHERE regduser= '$username' AND regdpass = '$regdpass'");

if (mysql_num_rows($result)!= 1) {   $error = "Login failed"; 
 #include "loginform.php"; 
} else { 
 $_SESSION['username'] = "$username"; 
 $_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; 
 #include "membersection.php"; 
}
?>

HTML

  <form action="inc/check_regUsr.php" method="post" id="userLogon">
        <div class="field required">
        Username: <input type="text" name="regduser" tabindex="1" /><br />
        </div>
        <div class="field required">
        Password: <input type="text" name="regdpass" tabindex="2" /><br />
        </div>
        <input type="submit" name="submitUser" />
    </form>

当我尝试提交有效凭据时,它显示: 警告:mysql_num_rows() 需要参数1 是资源,第 12 行给出的布尔值

为什么它不这样,我该如何修复它?

谢谢

The "dbcred.php" class

<?php
   # pdo_testdb_connect.php - function for connecting to the "test" database

   function testdb_connect ()
   {
     $dbh = new PDO("mysql:host=localhost;dbname=dbname", "root", "");
     return ($dbh);
   }
?>

PHP

<?php
#connect mysql
require_once "dbcred.php";
$dbh = testdb_connect ();

session_start(); 
$username = $_POST['regduser']; 
$userpass = md5($_POST['regdpass']); 
$result = mysql_query("SELECT * FROM Student WHERE regduser= '$username' AND regdpass = '$regdpass'");

if (mysql_num_rows($result)!= 1) {   $error = "Login failed"; 
 #include "loginform.php"; 
} else { 
 $_SESSION['username'] = "$username"; 
 $_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; 
 #include "membersection.php"; 
}
?>

HTML

  <form action="inc/check_regUsr.php" method="post" id="userLogon">
        <div class="field required">
        Username: <input type="text" name="regduser" tabindex="1" /><br />
        </div>
        <div class="field required">
        Password: <input type="text" name="regdpass" tabindex="2" /><br />
        </div>
        <input type="submit" name="submitUser" />
    </form>

When I try to submit valid credentials it says: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in on line 12

Why does it not like this and how can I fix it?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎无痕… 2024-12-04 08:57:00
$result = mysql_query("SELECT * FROM Student WHERE regduser= '$username' AND regdpass = '$regdpass'");

我不知道你的数据库类是如何工作的。所以我会写一个通用的mysql连接代码。

mysql_connect($host,$username,$password);
mysql_select_db($db);

不,您可以执行查询。

$result = mysql_query("SELECT * FROM Student WHERE regduser= '$username' AND regdpass = '$regdpass'");

I don't know how your database class is working. So I will write a general mysql connect code.

mysql_connect($host,$username,$password);
mysql_select_db($db);

No you can execute the query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文