【问题标题】:Can someone help me with some PHP Errors (relevant to MYSQL Queries)?有人可以帮我解决一些 PHP 错误(与 MYSQL 查询相关)吗?
【发布时间】:2015-01-28 01:06:45
【问题描述】:

我正在尝试将表单与 ipboard 数据库链接起来,但由于我是 PHP 编码的初学者,我一直在兜圈子,不知道该怎么做,只需要链接就可以了。

    29-Nov-2014 16:18:30 UTC] PHP Warning:  mysql_num_rows() expects parameter 1 to be resource, null given in /home/rise718c/public_html/Sean/Login/checklogin.php on line 40
        [29-Nov-2014 16:20:57 UTC] PHP Notice:  Undefined variable: link in /home/rise718c/public_html/Sean/Login/checklogin.php on line 40
        [29-Nov-2014 16:20:57 UTC] PHP Warning:  mysql_query() expects parameter 2 to be resource, null given in /home/rise718c/public_html/Sean/Login/checklogin.php on line 40
        [29-Nov-2014 16:20:57 UTC] PHP Warning:  mysql_num_rows() expects parameter 1 to be resource, null given in /home/rise718c/public_html/Sean/Login/checklogin.php on line 41

以上是错误日志

现在这是我要整理的代码**

<?php
$count = 0;
$host="localhost"; // Host name 
$username="login"; // Mysql username 
$password="password"; // Mysql password 
$db_name="dbname"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("dbname")or die("cannot select DB");

// username and password sent from form 
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword']; 

// encrypt password 

$encrypted_mypassword= md5( $mypassword ) ;
$sql="SELECT * FROM $tbl_name WHERE name='$myusername' and members_pass_hash='$encrypted_mypassword'";
$result=mysql_query($sql);


// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($encrypted_mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($encrypted_mypassword);
$sql="SELECT 'members_pass_hash','name' FROM $tbl_name WHERE name='$myusername' and members_pass_hash='$encrypted_mypassword'";
 mysql_real_escape_string($myusername);
 mysql_real_escape_string ($encrypted_mypassword);

 $result=mysql_query($sql);
 if (!$result) {
   die('Invalid query: ' . mysql_error());
}

// Mysql_num_row is counting table row
mysql_connect($host, $username, $password);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

如果您能帮助纠正我的错误,我们将不胜感激。

【问题讨论】:

标签: php mysql database hash


【解决方案1】:

您不需要多次连接到您的数据库。因为您这样做了,PHP 不知道要使用哪个连接,所以它将您的数据库句柄作为附加参数。

您的代码也可以很容易地用于控制您的服务器。您将用户名和密码作为字符串放入第一个查询中,因此您无法防止 sql 注入。

你应该使用一些 SQL 库来为你处理调用,其他人已经解决了这个问题。一种简单的方法是使用 PDO_mysql PHP 模块,该模块提供参数化查询。

另外请不要使用 md5,它有已知的弱点。使用 SHA256 或类似的东西,它只是一个不同的调用函数。 如果你想把它做好,你还必须担心填充。有些功能甚至可以为您做到这一点。看看http://php.net/manual/en/function.password-hash.php

【讨论】:

  • "使用 SHA256 或类似的,只是调用不同的函数。"呃呃呃呃呃呃呃没有
  • uuuhhhhhhhhmmmm 是的 hash('SHA256', 'password') 做到了
  • 不,你永远不应该将它用于密码散列
  • 我对 SHA256 密码的两分钱:stackoverflow.com/questions/11624372/…
猜你喜欢
  • 2021-07-10
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
  • 2015-02-25
  • 1970-01-01
相关资源
最近更新 更多