sqlsrv_query不会插入到数据库中

huangapple go评论50阅读模式
英文:

sqlsrv_query doesn't insert into database

问题

Select语句正常工作,但插入语句不起作用。$Username和$password存在。问题可能是什么?

    <?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'>用户已存在</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");
        }
    }
    ?>

编辑:使用以下代码来查看错误:

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

得到警告:Array to string conversion

password_hash 是一个数组,但使用 echo(reset($password_hash)); 会得到:Argument #1 ($array) must be of type array, string given

英文:

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

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

    }
}
?&gt;

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

答案1

得分: 1

密码的数据类型在数据库中是nvarchar(50),而password_hash包含超过50个字符,这是问题的原因。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月14日 21:17:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定