I'm new to php with mySql so sorry in advance if this is something obvious. When I try to add a new user using this code I always get my error message and never the success one. Can anyone see where I have gone wrong ?
Edit: "Adding echo mysqli_error($link)" gives me this error; "Field 'name' doesn't have a default value"
There are 4 columns in my database and name is the last one, can I add a user this way without specifying a 'name'
<?php
if (array_key_exists('email', $_POST) OR array_key_exists('password', $_POST)) {
$link = mysqli_connect("localhost", "root", "", "users");
if (mysqli_connect_error()){
die ("Error connecting");
}
if ($_POST['email'] == ''){
echo "<p>Email address is required</p>";
} else if ($_POST['password'] == ''){
echo "<p>Password is required</p>";
} else {
$query = "SELECT `id` FROM `users` WHERE email = '".mysqli_real_escape_string($link, $_POST['email'])."'";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) > 0) {
echo "<p>That Email address already has an account<p>";
} else {
$query = "INSERT INTO `users` (`email`, `password`) VALUES ('".mysqli_real_escape_string($link, $_POST['email'])."',
'".mysqli_real_escape_string($link, $_POST['password'])."')";
if (mysqli_query($link, $query)) {
echo "<p>You have been signed up!</p>";
} else {
echo "<p>There was a problem, please try again later</p>";
}
}
}
}
?>
HTML form :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
Form
</title>
</head>
<body>
<form method="post">
email<br>
<input type="text" name="email" id="email" value=""><br>
password<br>
<input type="password" name="password" id="email" value=""><br>
<input type="submit" value="Sign Up">
</form>
</body>
</html>
mysqli_real_escape_stringis not as secure. You should switch to using "prepared statements" if this is new code you are creating.echo mysqli_error($link);echo $queryis a great way to debug you can often just see the error or if not try the query in the likes of phpmyadmin