Can';不要用php打印支票
我对一些主题的印刷有意见。此问题是我无法更新用户信息的原因。虽然我认为这个代码是正确的 这是我在editprofile页面上的代码Can';不要用php打印支票,php,sql-update,Php,Sql Update,我对一些主题的印刷有意见。此问题是我无法更新用户信息的原因。虽然我认为这个代码是正确的 这是我在editprofile页面上的代码 include_once ("classes/Db.class.php"); include_once ("classes/config.class.php"); include_once ("classes/user.class.php"); session_start(); if (!isset($_SESSION['loggedIn'])) { ec
include_once ("classes/Db.class.php");
include_once ("classes/config.class.php");
include_once ("classes/user.class.php");
session_start();
if (!isset($_SESSION['loggedIn'])) {
echo("not set");
header("Location:index.php");
}
if (!empty($_POST['update'])) {
echo "test 2";
// todo: 1 form input velden ophalen
try {
$u = new User();
$u->Username = $_POST['form-username'];
$u->Email = $_POST['form-email'];
$u->Password = $_POST['form-password'];
$u->Passwordconfirmation = $_POST['form-passwordconf'];
$u->Update($_SESSION['loggedIn']);
$u->profileImg($_SESSION['loggedIn']);
$succes = "Je gegevens zijn aangepast";
} catch (exception $e) {
$succes = $e->getMessage();
}
}
下面是我的user.class.php中的代码。我想讨论的具体函数是更新函数
<?php
/**
* Created by PhpStorm.
* User: erhanlammar
* Date: 23/04/16
* Time: 10:13
*/
include_once("Db.class.php");
class User{
// todo: 1 private variabelen aanmaken voor firstname, lastname, ...
private $_db;
private $m_sUsername;
private $m_sFirstname;
private $m_sLastname;
private $m_sEmail;
private $m_sPassword;
private $m_sPasswordconfirmation;
private $m_sProfileimage;
//private $m_susersid;
// todo: 2 getters & setters!
public function __set($p_sProperty, $p_vValue){
switch($p_sProperty){
case "Username":
if(!empty($p_vValue)){
$this->m_sUsername = $p_vValue;
break;
}else{
//opvangen van leeg veld username.
throw new exception("vergeet geen username in te vullen");
}
case "Firstname":
if(!empty($p_vValue)){
$this->m_sFirstname = $p_vValue;
break;
}else{
//opvangen van leeg veld firstname.
throw new exception("Uw voornaam hebben we echt wel nodig");
}
case "Lastname":
if(!empty($p_vValue)){
$this->m_sLastname = $p_vValue;
break;
}else{
//opvangen van leeg veld lastname.
throw new exception("Heeft u geen achternaam?");
}
case "Email":
if(!empty($p_vValue)){
$this->m_sEmail = $p_vValue;
break;
}else{
//opvangen van leeg veld email.
throw new exception("Wij hebben uw email nodig om u op de hoogte te houden");
}
case "Password":
if(!empty($p_vValue)){
$this->m_sPassword = $p_vValue;
break;
}else{
//opvangen van leeg veld firstname.
throw new exception("Zonder wachtwoord geen login");
}
case "Passwordconfirmation":
if(!empty($p_vValue)){
$this->m_sPasswordconfirmation = $p_vValue;
break;
}else{
//opvangen van leeg veld firstname.
throw new exception("Zonder wachtwoord geen login");
}
case "Profileimage":
$this->m_sProfileimage = $p_vValue;
break;
}
}
public function __get($p_sProperty){
switch($p_sProperty){
case "Username":
return $this->m_sUsername;
break;
case "Firstname":
return $this->m_sFirstname;
break;
case "Lastname":
return $this->m_sLastname;
break;
case "Email":
return $this->m_sEmail;
break;
case "Password":
return $this->m_sPassword;
break;
case "Passwordconfirmation":
return $this->m_sPasswordconfirmation;
break;
case "Profileimage":
return $this->m_sProfileimage;
break;
}
}
private function checkPasswordConfirmation(){
if($this->m_sPassword == $this->m_sPasswordconfirmation){
return true;
}else{
throw new exception("wachtwoorden komen niet overeen");
}
}
public function signup(){
if(!$this->checkEmail()){
throw new exception("Dit emailadres bestaat al neem een ander of ga naar login");
}
if(!$this->checkUsername()){
throw new exception("De username die u gekozen heeft bestaat al!!");
}
if(!$this->checkPasswordConfirmation()){
throw new exception("De registratie is niet correct verlopen. Check alles nog eens");
}
$conn = new PDO("mysql:host=localhost;dbname=IMDstagram", "root","");
$options= ['cost' => 12];
$this->m_sPassword = password_hash($this->m_sPassword, PASSWORD_DEFAULT, $options);
$statement = $conn->prepare("INSERT INTO users(
username,
firstname,
lastname,
email,
password
)
VALUES(
:username,
:firstname,
:lastname,
:email,
:password
)
");
$statement->bindValue(":username", $this->m_sUsername);
$statement->bindValue(":firstname", $this->m_sFirstname);
$statement->bindValue(":lastname", $this->m_sLastname);
$statement->bindValue(":email", $this->m_sEmail);
$statement->bindValue(":password", $this->m_sPassword);
return $statement->execute();
}
public function checkEmail(){
$PDO = Db::getInstance();
$stmt = $PDO->prepare("SELECT * FROM users WHERE email= :email");
$stmt->bindValue(":email", $this->m_sEmail, PDO::PARAM_STR);
$stmt->execute();
if( $stmt->rowCount() > 0 ){
return false;
throw new exception( "" ) ;
}
else{
return true;
}
}
public function checkUsername(){
$PDO = Db::getInstance();
$stmt = $PDO->prepare("SELECT * FROM users WHERE username= :username");
$stmt->bindValue(":username", $this->m_sUsername, PDO::PARAM_STR);
$stmt->execute();
if( $stmt->rowCount() > 0 ){
return false;
throw new exception( "" ) ;
}
else{
return true;
}
}
public function loggingIn(){
if(!empty($this->m_sUsername) && !empty($this->m_sPassword)){
$PDO = Db::getInstance();
$stmt = $PDO->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindValue(":username", $this->m_sUsername, PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount() > 0){
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$password = $this->m_sPassword;
$hash = $result['password'];
if(password_verify($password, $hash)){
session_start();
$_SESSION["loggedIn"] = $result['usersid'];
$_SESSION["loggedIn"] = $result ['username'];
session_write_close();
return true;
}else{
return false;
}
}
}
}
public function Update($userid){
$PDO = Db::getInstance();
if(!empty($this->m_sUsername)){
$stmt = $PDO->prepare("UPDATE users SET username= :username WHERE usersid = :usersid");
$stmt->bindValue(":usersid", $userid, PDO::PARAM_INT );//update velden velden met where m_sUserid = Userid
$stmt->bindValue(":username", $this->m_sUsername, PDO::PARAM_STR);
$stmt->execute();
echo("username");
}
if (!empty($this->m_sEmail)){
if(!$this->checkEmail()){
throw new exception("De update is niet correct verlopen. Check alles nog eens");
}
$stmt = $PDO->prepare("UPDATE users SET email= :email WHERE usersid = :usersid");
$stmt->bindValue(":usersid", $userid, PDO::PARAM_INT );//update username met " " "
$stmt->bindValue(":email", $this->m_sEmail, PDO::PARAM_STR); //aleen email
$stmt->execute();
echo("email");
}
if (!empty($this->m_sPassword)){
if(!$this->checkPasswordConfirmation()){
throw new exception("de update lukt niet, passwoorden komen niet overeen.");
}
$stmt = $PDO->prepare("UPDATE users Set password = :password WHERE usersid = :usersid");
$stmt->bindValue(":usersid", $userid, PDO::PARAM_INT );//update password met " " "
$stmt->bindValue(":password", $this->m_sPassword, PDO::PARAM_STR); //aleen u password
$stmt->execute();
}
}
}
不要在三个if
块中分别更新username
、email
和password
字段,而只使用一个if
块来更新所有三个字段
因此,您的update()
方法应该如下所示:
public function Update($userid){
$PDO = Db::getInstance();
if(!empty($this->m_sUsername) && !empty($this->m_sEmail) && !empty($this->m_sPassword) && !empty($this->m_sPasswordconfirmation)){
if($this->m_sPassword == $this->m_sPasswordconfirmation){
$options= ['cost' => 12];
$this->m_sPassword = password_hash($this->m_sPassword, PASSWORD_DEFAULT, $options);
$stmt = $PDO->prepare("UPDATE users SET username= :username, email = :email, password = :password WHERE usersid = :usersid");
$stmt->bindValue(":usersid", $userid, PDO::PARAM_INT );
$stmt->bindValue(":username", $this->m_sUsername, PDO::PARAM_STR);
$stmt->bindValue(":email", $this->m_sEmail, PDO::PARAM_STR);
$stmt->bindValue(":password", $this->m_sPassword, PDO::PARAM_STR);
if($stmt->execute()){
// success
echo "succes";
}else{
// error
echo "failure";
}
}else{
// Mismatch password
echo "mismatch password";
}
}else{
// some values are not set
echo "some values are not set";
}
}
不要在三个if
块中分别更新username
、email
和password
字段,而只使用一个if
块更新所有三个字段
因此,您的update()
方法应该如下所示:
public function Update($userid){
$PDO = Db::getInstance();
if(!empty($this->m_sUsername) && !empty($this->m_sEmail) && !empty($this->m_sPassword) && !empty($this->m_sPasswordconfirmation)){
if($this->m_sPassword == $this->m_sPasswordconfirmation){
$options= ['cost' => 12];
$this->m_sPassword = password_hash($this->m_sPassword, PASSWORD_DEFAULT, $options);
$stmt = $PDO->prepare("UPDATE users SET username= :username, email = :email, password = :password WHERE usersid = :usersid");
$stmt->bindValue(":usersid", $userid, PDO::PARAM_INT );
$stmt->bindValue(":username", $this->m_sUsername, PDO::PARAM_STR);
$stmt->bindValue(":email", $this->m_sEmail, PDO::PARAM_STR);
$stmt->bindValue(":password", $this->m_sPassword, PDO::PARAM_STR);
if($stmt->execute()){
// success
echo "succes";
}else{
// error
echo "failure";
}
}else{
// Mismatch password
echo "mismatch password";
}
}else{
// some values are not set
echo "some values are not set";
}
}
1) 检查以下实例属性$m_sUsername
、$m_sEmail
和$m_sPassword
是否存在和设置。您已经设置了不同的实例属性$u->Username=…
,$u->Email=…
等。2)重构update()方法,而不是三个if块仅使用一个if块更新所有三个字段。3) 不要将密码存储为纯可读文本,请始终在插入之前存储。它们都已设置并存在。密码也是散列的。您在哪里设置了所有这些实例属性,$m_sUsername
、$m_sEmail
和$m_sPassword
,您在哪里散列了密码?我在任何地方都看不到代码。那么,如果update()
方法的块出现问题,您会在哪个块中遇到问题?我在这里看不到任何关键问题,除了两件事,1)您应该使用一个if块来更新所有三个字段2)我看不到这条语句$options=['cost'=>12]$this->m\u sPassword=password\u hash($this->m\u sPassword,password\u DEFAULT,$options)
在update()
方法中,这意味着您在更新之前没有对密码进行哈希运算;在我单击更新的提交按钮后不会显示。首先,我将检查您告诉我的两件事是否能够解决此问题。1)检查以下实例属性是否存在并设置了。您已经设置了不同的实例属性$u->Username=…
,$u->Email=…
等。2)重构update()方法,而不是三个if块仅使用一个if块更新所有三个字段。3) 不要将密码存储为纯可读文本,请始终在插入之前存储。它们都已设置并存在。密码也是散列的。您在哪里设置了所有这些实例属性,$m_sUsername
、$m_sEmail
和$m_sPassword
,您在哪里散列了密码?我在任何地方都看不到代码。那么,如果update()
方法的块出现问题,您会在哪个块中遇到问题?我在这里看不到任何关键问题,除了两件事,1)您应该使用一个if块来更新所有三个字段2)我看不到这条语句$options=['cost'=>12]$this->m\u sPassword=password\u hash($this->m\u sPassword,password\u DEFAULT,$options)
在update()
方法中,这意味着您在更新之前没有对密码进行哈希运算;在我单击更新的提交按钮后不会显示。首先,我将检查您告诉我的两件事是否可以解决它。谢谢,该功能正常。现在,更新不会在我的系统中发生database@Lammar_E检查update()
方法中$userid
的状态。Doecho$userid
insideupdate()doorgevoerd@Lammar_E那么Jacky是正确的用户id吗?对照要更新的表的特定行检查此项。@Lammar_E请参阅此语句$stmt->bindValue(“:usersid”,$userid,PDO::PARAM_INT)代码>,您已将用户id绑定为整数。所以首先更改这个东西,它应该是$stmt->bindValue(“:usersid”,$userid,PDO::PARAM_STR)再次测试你的应用程序。谢谢,该功能可以正常工作。现在,我的应用程序中不会出现更新database@Lammar_E检查update()
方法中$userid
的状态。Doecho$userid
insideupdate()doorgevoerd@Lammar_E那么Jacky是正确的用户id吗?对照要更新的表的特定行检查此项。@Lammar_E请参阅此语句$stmt->bindValue(“:usersid”,$userid,PDO::PARAM_INT)代码>,您已将用户id绑定为整数。所以首先更改这个东西,它应该是$stmt->bindValue(“:usersid”,$userid,PDO::PARAM_STR)代码>并再次测试应用程序。