英文:
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?
<?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
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论