[英]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.