Search code examples
phpsqlsrv

sqlsrv_query doesn't insert into database


Select statement is working but insert into is not. $Username and $password exists. What could be the problem?

<?php
session_start();
include('config.php');
if (isset($_POST['register'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    $password_hash = password_hash($password, PASSWORD_BCRYPT);         
    $query = "SELECT * FROM k WHERE su=?";
    $params = array($username);
    $options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
    $stmt = sqlsrv_query( $conn, $query, $params,$options);
    $row_count = sqlsrv_num_rows( $stmt );
    if ($row_count> 0) {
          $msg="<span style='color:red'>USER EXISTS</span>";    
    }
    else if ($row_count == 0) {
        $options2 =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
        $query2 = "INSERT INTO k (su,s) VALUES (?,?)";
        $params2 = array($username,$password_hash);
        $stmt2= sqlsrv_query($conn, $query2,$params2);
        //header("location:giris.php");

    }
}
?>

Edit: used $errors = sqlsrv_errors(); foreach($errors as $error) { echo $error;}

to see errors and got Warning: Array to string conversion password_hash is Array$2y$10$r3G2... but with echo(reset($password_hash)); result is : Argument #1 ($array) must be of type array, string given


Solution

  • Frustrating, but the data type of password was nvarchar(50) in database, and password_hash consists of more than 50 chars. This was the issue.