Update MYSQL does not work correctly

1

I have this code:

<?php
session_start();
if (!isset($_SESSION['user'])){ header("Location: index.php");}
include("db_files/db.php");
include("inc/functions.php");
$oldpass = mysqli_real_escape_string($db, $_POST['oldpass']);
$oldcrypt = encripta_password(mysqli_real_escape_string ($db, $oldpass));

$newpass = encripta_password(mysqli_real_escape_string($db, $_POST['newpass']));
$renewpass = encripta_password(mysqli_real_escape_string($db, $_POST['renewpass']));

$usermail = mysqli_real_escape_string($db, $_SESSION['user']);

$strSQL = "SELECT password FROM usuarios WHERE email = '".$usermail."'";
$query = mysqli_query($db, $strSQL);
$result = mysqli_fetch_array ($query);
if ($result['password'] == $oldcrypt){
	if ($newpass == $renewpass){
		$strSQL = "UPDATE usuarios SET usuarios.password = '".$newpass."' WHERE usuarios.email = '".$usermail."'";
		$query = mysqli_query($db, $strSQL);
		if ($db->query($query) === TRUE) {
			echo "Updated";
		}else{
			echo "Not updated". $db->error."<br>";
		}
	}
}else{
	echo "No match password";
}
// $pass = encripta_password("asdasd");
// echo $pass;
?>

And when I run it, I get this error:

  

Not updatedYou have an error in your SQL syntax; check the manual that   corresponds to your MariaDB server version for the right syntax to use   near '1' at line 1

This same update in the phpmyadmin works correctly (substituting the variables for their value)

I add that if it does the update but I do not know where that error comes from.

This is the encripta_password function:

 function encripta_password($password)
{
    //$salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
    $saltedPW =  $password . PANDAERP_HASH;
    $hashedPW = hash('sha256', $saltedPW);
    return $hashedPW;
}
define("PANDAERP_HASH", "16a507d6f3da37d8ba00b28bf622d144cba96f65d3a18f8b15911697d6409f0f");
    
asked by Pavlo B. 04.11.2016 в 16:35
source

1 answer

6

Your error is in that you try to execute the query twice, I explain in comments:

        //esto ejecuta el query una vez y devuelve 1 (éxito)            
        $query = mysqli_query($db, $strSQL);
        //esto intenta ejecutar el 1 como query nuevamente.
        if ($db->query($query) === TRUE) {

Leave only the first, or change the second by passing the string containing your SQL statement.

    
answered by 04.11.2016 / 16:47
source