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. 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. Any help would be much appreciated, Thanks in advance!