普通网友 2016-09-05 08:00
浏览 36

PHP mysqli_query()到PDO

I am trying to make a page to change a password in the database. I made the form and this is the PHP code :

if(isset($_POST['btn-newpass']))
{
$username = strip_tags($_POST['username']);
$password = md5(strip_tags($_POST['password']));
$password_new = md5(strip_tags($_POST['password_new']));
$password_new_conf = md5(strip_tags($_POST['password_new_conf']));
$password_in_db= mysqli_query("SELECT password FROM utilizatori WHERE username='$username'");

if(!$password_in_db)
{ echo "The entered username doesn't exist";}
elseif($password!=$password_in_db)
{ echo "The current password is wrong";}

if($password_new == $password_new_conf)
{$sql = mysqli_query("UPDATE utilizatori SET password='$password_new' WHERE username='$username'");}

if($sql)
{ echo "Changed successfully!";}
 else
{ echo "The passwords do not match";}
}

When I try to change a password I get the following errors:

Warning: mysqli_query() expects at least 2 parameters, 1 given in      A:\XAMPP\htdocs\testing\change_password.php on line 10
The entered username doesn't exist
Warning: mysqli_query() expects at least 2 parameters, 1 given in A:\XAMPP\htdocs\testing\change_password.php on line 18
Passwords do not match

In connection.php I have the following code:

class Database
{   
private $host = "localhost";
private $db_name = "atlx";
private $username = "root";
private $password = "";
public $conn;

public function dbConnection()
{

    $this->conn = null;    
    try
    {
        $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
        $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
    }
    catch(PDOException $exception)
    {
        echo "Connection error: " . $exception->getMessage();
    }

    return $this->conn;
}
}

Could somebody point me out what is wrong here?

EDIT:

I realised the connection to the database is done using PDO. How can I convert the PHP code to work with PDO?

  • 写回答

5条回答 默认 最新

  • doushishi2415 2016-09-05 08:04
    关注

    In the mysqli_query you have to pass the connection variable to for executing

    Replace

    $password_in_db= mysqli_query("SELECT password FROM utilizatori WHERE username='$username'")
    

    With

    $password_in_db= mysqli_query($con,"SELECT password FROM utilizatori WHERE username='".$username."'")
    
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备