I wanted to create a login screen that a user can choose which database to save to depending on their profession via radio buttons. I've got most errors ironed out, but these ones:
Notice: Undefined variable: sqlinsert in C:\xampp\htdocs\project\save.php on line 34
Warning: mysqli_query(): Empty query in C:\xampp\htdocs\project\save.php on line 34
have refused to go away. Here's my Html:
<form method="post" action="http://localhost/project/save.php">
<legend align="left">Login</legend>
<input type="text" placeholder="Username" name="uname"><br>
<input type="password" placeholder="Password" name="password"><br>
<p>
<label>
<input type="radio" name="dbchoice" value="admin" id="RadioGroup1_0">
Admin</label>
<br>
<label>
<input type="radio" name="dbchoice" value="expert" id="RadioGroup1_1">Mental health experts</label>
<br>
</p>
<input type="submit" value="Save" name="save">
<input type="reset" value="Cancel">
</form>
And here's my PHP:
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$db = "project";
//connect to the database using procedural method
$dbconnect = mysqli_connect($hostname,$username,$password,$db);
if(!$dbconnect){
echo "an error occured while connecting to the database";
}
if(isset($_POST['save'])){
//get values from the form
$username = $_POST['uname'];
$password = $_POST['password'];
//write the SQL STATEMENT FOR SAVING - INSERT
if (isset($dbchoice) && $dbchoice==$_POST["admin"]) {
$sqlinsert = "INSERT INTO administrator
(username, password)
VALUES('$username','$password')";
}elseif (isset($dbchoice) && $dbchoice==$_POST["expert"]){
$sqlinsert = "INSERT INTO experts
(username, password)
VALUES('$username','$password')";
}else{
echo "An error occured while writing to database.";
}
if(mysqli_query($dbconnect,$sqlinsert)){
echo "<script type='text/javascript'> alert('Data saved successfully');</script>";
header('location: index.php');
}else{
echo "An error occured while saving";
}
//close the connection
mysqli_close($dbconnect);
}else{
header('location: index.php');
}
?>