doumo0206
2019-07-05 05:36
浏览 123
已采纳

为什么在将数据插入SQL服务器时我的php产生错误?

I'm trying to get PHP to insert POST data from my html form into my SQL Server 2018 on a local machine. I looked at php documentation and follow it but i still don't get it why the code is producing an error. The table name that i'm inserting the data to is called "USER". Can anyone look at my code and tell me what's wrong?

I have looked various sources including PHP.net and stack overflow for solution but doesn't help me in solving my problem.

PHP script:

if(isset($_POST['create_acc'])) {

    $username = strip_tags(trim($_POST["username"]));
    $password = strip_tags(trim($_POST["password"]));
    $role = strip_tags(trim($_POST["role"]));
    $pwdhash = password_hash($password, PASSWORD_ARGON2I);


    $sql = "INSERT INTO USER (user_id, password, role) VALUES (?, ?, ?)";
    $params = array($username, $pwdhash, $role);
    $stmt = sqlsrv_query( $conn, $sql, $params);

    if ($stmt){

        $msg = "Account created successfully!";

    }else{

        die(print_r(sqlsrv_errors(), true));
    }

}

Result:

Array ( 
    [0] => Array ( 
        [0] => 42000 
        [SQLSTATE] => 42000 
        [1] => 156 
        [code] => 156 
        [2] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near the keyword 'USER'. 
        [message] => [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near the keyword 'USER'. 
    ) 
)

图片转代码服务由CSDN问答提供 功能建议

我正在尝试让PHP将html表单中的POST数据插入本地计算机上的SQL Server 2018 。 我查看了php文档并遵循它但我仍然不明白代码产生错误的原因。 我正在插入数据的表名称称为“USER”。 任何人都可以查看我的代码并告诉我出了什么问题吗?

我看了很多来源,包括PHP.net和堆栈溢出解决方案但是没有帮助我解决我的问题。

PHP脚本:

  if(isset($ _ POST ['create_acc'])){
 
 $ username = strip_tags(trim  ($ _POST [“username”])); 
 $ password = strip_tags(trim($ _ POST [“password”])); 
 $ role = strip_tags(trim($ _ POST [“role”])); \  n $ pwdhash = password_hash($ password,PASSWORD_ARGON2I); 
 
 
 $ sql =“INSERT INTO USER(user_id,password,role)VALUES(?,?,?)”; 
 $ params = array($  username,$ pwdhash,$ role); 
 $ stmt = sqlsrv_query($ conn,$ sql,$ params); 
 
 if if($ stmt){
 
 $ msg =“帐户创建成功!”;  
 
} else {
 
 die(print_r(sqlsrv_errors(),true)); 
} 
 
} 
   
 
 

结果:

 数组(
 [0] =>数组(
 [0] => 42000 
 [SQLSTATE] => 42000 
 [1] =  > 156 
 [code] => 156 
  [2] =>  [Microsoft] [SQL Server的ODBC驱动程序17] [SQL Server]关键字“USER”附近的语法不正确。  
 [消息] =>  [Microsoft] [SQL Server的ODBC驱动程序17] [SQL Server]关键字“USER”附近的语法不正确。  
)
)
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzi9211 2019-07-05 06:18
    已采纳

    If you are using a reserved keyword in SQL Server (which you can check here). You have to use delimited identifiers.

    This should work:

    $sql = "INSERT INTO [USER] (user_id, password, role) VALUES (?, ?, ?)";
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsfsdfsdfsdf6455 2019-07-05 05:42

    I just found out 1 min later after posting the questions. "User" is a Reserved Keywords in SQL Server. Once i changed my table name to USERNAME then it works!!

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题