There are multiple things to note in this piece of code.
mysqli_error("error connecting to database")
is wrong, that function takes the connection as an argument, not a string.
- You should use
mysqli_connect_error()
for checking for connection-errors, not mysqli_error()
- You should add
error_reporting(E_ALL); ini_set('display_errors', 1);
at the top of your file, so you can find the actual errors.
- You should take advantage of using prepared statements in your queries, specially those containing PHP variables/user input.
- Unless you've got an
Array
class, you don't initialize a new array with new Array()
, but just $resultset = array();
- Some proper indention makes the code easier to read.
- Don't store your passwords in plain-text! This is not secure at all! PHP has built-in functions which you should use to handle storing of passwords, see the
password_hash()
function
which is a lot more secure!
With these improvements, the code should work - unless there are other errors not visible to us. In any case, it would be easier to figure out what's not working. You should also read up on, and implement points 4 and 7.
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(isset($_POST['name']) && isset($_POST['password']))
{
$sql = "SELECT * FROM kandidaat WHERE name = '".$_POST['name']."' AND password = '".$_POST['password']."' LIMIT 1;";
$conn = mysqli_connect("localhost", "user", "pw", "db") or die(mysqli_connect_error());
if ($result = $conn->query($sql)) {
$resultset = array();
if ($result->num_rows > 0) {
$resultset = $result;
session_start();
$_SESSION['login_user']= $_POST['name'];
} else {
$resultset = "0";
}
} else {
echo "Query failed: ".mysqli_error($conn);
}
}
?>
Note: You shouldn't display errors on your live site, just while in development. Errors can be used to find vulnerabilities and exploit your code.
Readingmaterial