无法使用 php 和 Mysql 更新数据库
Posted
技术标签:
【中文标题】无法使用 php 和 Mysql 更新数据库【英文标题】:Can't update database using php and Mysql 【发布时间】:2014-11-23 11:48:18 【问题描述】:我正在尝试使用以下代码更新数据库中的用户密码
<?php
session_start();
if( isset($_SESSION['user']) )
else
header("location: index.php");
$host = "localhost";
$username = "xxxx";
$password = "xxxxx";
$db_name = "auth_db";
$tbl_name = "users";
$link = new mysqli("$host", "$username" , "$password", "$db_name");
if(mysqli_connect_error())
die('Connect Error ('.mysqli_connect_errno().')' .msqli_connect_error());
$username = $_SESSION['user'];
$pwd = $_POST['oldpass'];
$pwd1 = $_POST['newpass'];
$pwd2 = $_POST['newpass1'];
if($pwd1 !== $pwd2)
Print '<script>alert("New Passwords do not match");</script>';
Print '<script>window.location.assign("pwd.php");</script>';
$query = mysqli_query($link, "SELECT * from users WHERE username = '$username'");
$user_exist = mysqli_num_rows($query);
$tbl_user = "";
$tbl_password = "";
$password = 0 ;
if($user_exist > 0)
while($row = mysqli_fetch_assoc($query))
$tbl_user = $row['username'];
$tbl_password = $row['password'];
$password = password_verify($pwd, $tbl_password);
if(($username == $tbl_user) && ($password))
if($password)
$new_hash = password_hash(('$pwd1'), PASSWORD_BCRYPT);
mysqli_query($link, "UPDATE $tbl_name SET password = '$new_hash' WHERE username = '$tbl_user'");
Print '<script>alert("Updated, Please relogin.");</script>';
Print '<script>window,location.assign("logout.php");</script>';
else
Print '<script>alert("Incorrect Password");</script>';
Print '<script>window,location.assign("pwd.php");</script>';
?>
我能够生成哈希,但它没有在数据库中更新,页面被重定向到给定的链接。我在想我的衣服有些磨损
mysqli_query($link, "UPDATE $tbl_name SET password = '$new_hash' WHERE username = '$tbl_user'");
感谢任何帮助。谢谢。
【问题讨论】:
$new_hash = password_hash(('$pwd1'), PASSWORD_BCRYPT); 不,仍然没有工作。当我使用 mysqli_error() "Champ 'admin' inconnu dans where clause" 时出现此错误 密码正在更新,但不是给定的,而且我也收到此错误,Erreur de syntaxe pr�s de '1' � la ligne 1。 知道了,我必须更改 $new_hash = password_hash(('$pwd1'), PASSWORD_BCRYPT);到 $newhash = password_hash(($_POST['newpass']), PASSWORD_BCRYPT);感谢您的帮助。 【参考方案1】:试试这个
$link = new mysqli($host, $username , $password, $db_name);
mysqli_query($link, "UPDATE $tbl_name SET pasword = ".$new_hash." WHERE username = ".$tbl_user.");
【讨论】:
给出错误解析错误:语法错误,意外'" WHERE username = "' (T_CONSTANT_ENCAPSED_STRING)【参考方案2】:您的代码有很多语法错误。我已经引用了一些,并把它放在 cmets 中,这样你就可以自己更改了。
<?php
session_start();
if( isset($_SESSION['user']) )
else
header("location: index.php");
$host = "localhost";
$username = "xxxx";
$password = "xxxxx";
$db_name = "auth_db";
$tbl_name = "users";
$link = new mysqli("$host", "$username" , "$password", "$db_name");
if(mysqli_connect_error())
die('Connect Error ('.mysqli_connect_errno().')' .msqli_connect_error());
$username = $_SESSION['user'];
$pwd = $_POST['oldpass'];
$pwd1 = $_POST['newpass'];
$pwd2 = $_POST['newpass1'];
if($pwd1 !== $pwd2)
Print '<script>alert("New Passwords do not match");</script>';
Print '<script>window.location.assign("pwd.php");</script>';
$query = mysqli_query($link, "SELECT * from users WHERE username = '$username'");
$user_exist = mysqli_num_rows($query);
$tbl_user = ""; // instead of reinitializing these as a blank slate just use the unset(); function
$tbl_password = ""; // so its unset($tbl_user); so you can save memory.
$password = 0 ;
if($user_exist > 0)
while($row = mysqli_fetch_assoc($query))
$tbl_user = $row['username'];
$tbl_password = $row['password'];
$password = password_verify($pwd, $tbl_password);
if(($username == $tbl_user) && ($password))
if($password)
$new_hash = password_hash(('$pwd1'), PASSWORD_BCRYPT);
mysqli_query($link, "UPDATE $tbl_name SET password = '$new_hash' WHERE username = '$tbl_user'");
Print '<script>alert("Updated, Please relogin.");</script>';
Print '<script>window,location.assign("logout.php");</script>'; //<- window.location.assign();
else
Print '<script>alert("Incorrect Password");</script>';
Print '<script>window,location.assign("pwd.php");</script>'; //<-- window.location.assign();
?>
【讨论】:
谢谢,通过更改 $new_hash = password_hash(('$pwd1'), PASSWORD_BCRYPT);到 $new_hash = password_hash(($_POST['newpass']), PASSWORD_BCRYPT); ,但很奇怪。 window,location.assign() 与 window.location.assign() 的工作方式相同。 很高兴听到你让它工作:) 它不能与 hash(('$pwd1',password_bcrypt); 一起工作的原因是因为你在 ('$pwd1 ') :D 需要对这些小的语法错误有敏锐的观察力!感谢让它工作以上是关于无法使用 php 和 Mysql 更新数据库的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 PHP MySQL 更新数据库,其中数据来自 AngularJS1.2 服务