简体   繁体   English

如何使用 PHP 将用户输入插入 Microsoft Sql 服务器数据库?

[英]How do I insert user inputs into Microsoft Sql Server database using PHP?

I'm trying to allow users to create an account on my website and have their data stored in my connected database.我试图让用户在我的网站上创建一个帐户并将他们的数据存储在我连接的数据库中。 I've been able to establish a connection and pull data from the database and display it on my website, but I cannot figure out how to insert any data.我已经能够建立连接并从数据库中提取数据并将其显示在我的网站上,但我不知道如何插入任何数据。 Here's my code:这是我的代码:

<form class"login-form" action="phptest.php" method="POST">
   <input type="text" name="username">
   <input type="submit">Create Account</input>
</form>

<?php
<-- establish connection do database-1 -->
   $serverName = "database-1";
   $connectionInfo = array("Database"=>"maindb", "UID"=>"admin", "PWD"=>"rootuser");
   $conn = sqlsrv_connect($serverName, $connectionInfo);

<!-- assign entered username to a variable and display on webpage -->
   $username = $_POST['username'];
   echo $username;

<!-- insert username into database -->
   if ($_SERVER['REQUEST METHOD'] == 'POST'){
      $sql = "INSERT INTO Accounts (username) VALUES ('$username')";
      $stmt = sqlsrv_query($conn, $sql);
      if ($stmt === false){
         die(print_r(sqlsrv_errors(), true));
      }
      if (sqlsrv_fetch($stmt) === false){
         die(print_r(sqlsrv_errors(), true));
      }
   }
?>

I am able to display the inputed data on to the webpage so I know that's working, but when I log on to my database in Sql Server Management Studio there is no new data in my Accounts table.我能够在网页上显示输入的数据,所以我知道这是有效的,但是当我登录到 Sql Server Management Studio 中的数据库时,我的Accounts表中没有新数据。 There's a lot of examples on how to do this on MySql but I can't find any examples when it comes to Microsoft Sql Server.在 MySql 上有很多关于如何执行此操作的示例,但在涉及 Microsoft Sql 服务器时我找不到任何示例。 Any help would be much appreciated, Thanks in advance!任何帮助将不胜感激,在此先感谢!

use PDO you can check php manual at php.net https://www.php.net/manual/en/ref.pdo-dblib.php use PDO you can check php manual at php.net https://www.php.net/manual/en/ref.pdo-dblib.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用php脚本将图像插入ms sql服务器(我可以在mysql中执行但不能在ms sql中执行) - how to insert image into ms sql server using php script(I can able to do in mysql but not in ms sql) How can I insert into a table both a select max function from another table and variables input by the user, using PHP, SQL and Microsoft Access? - How can I insert into a table both a select max function from another table and variables input by the user, using PHP, SQL and Microsoft Access? 如何使用php,jquery和ajax将数据插入SQL Server数据库 - How to insert data into SQL Server database using php, jquery and ajax 如何使用PHP将数组插入SQL Server数据库 - How to insert an array using PHP to SQL Server Database 使用wpdb通过PhP插入SQL SERVER数据库 - using wpdb to insert into SQL SERVER database with PhP 如何使用PHP连接Microsoft SQL数据库 - How to connect Microsoft SQL Database using PHP 如何在用户输入中转义特殊字符-我正在使用SQL Server数据库 - How do I escape special characters in user input - I'm using sql server database 使用ajax和php插入数据库后,如何清除输入? - how can i clear inputs after insert into database with ajax and php? 如何使用MySQL将数组插入php中的数据库? - How do I insert an array into a database in php using MySQL? 如何使用PHP将UNIX时间戳插入oracle数据库 - How do I insert a UNIX timestamp into oracle database using PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM