I am trying to input data into a database with PHP and mysqli. There is no error and the code runs, but when I look in the database, it says that a row has been added but the columns "usernames" and "passwords" are empty.
$sql = "INSERT INTO $table (usernames, passwords) VALUES ('$username','$password')";
if(mysqli_query($db, $sql)) {
echo "new user successfully created";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($db);
}
I think there is something wrong above but I am not sure what it is.
EDIT: I think the problem is with $username
and $password
not having any value. It might have to do with the following code:
if ($_SERVER["REQUEST_METHOD"] == "POST") { //remove whitespace, slashes, and unnecessary characters
$username = test_input($_POST["username"]);
$password = test_input($_POST["password"]);
}
function test_input($data) { //function to remove unnecessary characters
if($data != trim($data)) {
die("error: there is an unnacceptable character in your username or password");
}
if($data != stripslashes($data)) {
die("error: you cannot have slashes");
}
if($data != htmlspecialchars($data)) {
die("error: password not accepted");
}
}
$username
and$password
values before insert query – Diego Mariani 47 mins agoecho $sql;
– Dagon 45 mins agoerror_reporting(E_ALL); ini_set('display_errors', 1);
and use a conditional!empty()
on wherever those variables are assigned as. Sure hope you're hashing this password, one that's of "this century". – Fred -ii- 45 mins ago