<?php
include("config.php"); // Just connects the database as $db
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$myusername = 'a';
$mypassword = 'a';
$sql = "SELECT id FROM users WHERE username = '$myusername'";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$active = $row['active'];
$count = mysqli_num_rows($result);
if(count == 0) {
$sql = "insert into users values (NULL, '$myusername', '$mypassword');";
$result = mysqli_query($db,$sql);
echo "succesful";
exit();
}
echo "Username already taken";
exit();
}
?>
Basically. I want it to NOT create another row if username is already found. The table is
id username password
1 a b
2 s a
The username is unique and the id autoincrements.
The problem with above code is that it always creates a row even if username is taken. For some reason $count = mysqli_num_rows($result); doesn't output anything but 0, why?
Consider the table I gave and run that code again. It should output "Username is already taken" instead it creates another row such that
id username password
1 a b
2 s a
3 a a