I'm making a small website, that works like diary, just for practice. I work with PHP and MYSQL. I've just started, I don't have any design or so, just the PHP-code and the form, thats the Code:
<?php
session_start();
if(array_key_exists("submit",$_POST))
{
$link = mysqli_connect("abcdefg", "abcdefg", "abcdefg", "abcdefg");
if(mysqli_connect_error()){
die("Connection to the database could not be established.");
}
$error = "";
if(!$_POST['email'])
{
$error .= "The email address is missing, please enter an email address.<br>";
}
if(!$_POST['password'])
{
$error .= "The password is missing, you need a password.<br>";
}
if($error != "")
{
$error = "<p>There was an error in your form. </p>".$error;
}
else
{
$query = "SELECT id FROM sd_users WHERE email = '".mysqli_real_escape_string($link, $_POST['email'])."' LIMIT 1";
$result = mysqli_query($link, $query);
if(mysqli_num_rows($result) > 0)
{
$error = "This email is already registered.";
}
else
{
$query = "INSERT INTO sd_users (email, password) VALUES ('".mysqli_real_escape_string($link, $_POST['email'])."',
'".mysqli_real_escape_string($link, $_POST['password'])."')";
mysqli_query($link, $query);
if(!mysqli_query($link, $query))
{
$error = "<p>You could not register, please try again.</p>";
}
else
{
$query = "UPDATE `sd_users` SET password = '".md5(md5(mysqli_insert_id($link)).$_POST['email'])."'WHERE id = ".mysqli_insert_id($link)." LIMIT 1";
mysqli_query($link, $query);
$_SESSION['id'] = mysqli_insert_id($link);
if($_POST['stayLoggedIn'] == '1')
{
setcookie("id", mysqli_insert_id($link), time() + 60*60*24*365*100);
}
echo "You have successfully registered!";
}
}
}
}
?>
<form method = "post">
<div id ="error"><?php echo $error; ?></div>
<input type = "email" name = "email" placeholder = "Your emailadress">
<input type = "password" name = "password" placeholder = "Password">
<input type = "checkbox" name = "stayLoggedIn" value = 1>
<input type = "submit" name = "submit" placeholder = "Sign Up!">
</form>
If you have a look at this part:
else
{
$query = "INSERT INTO sd_users (email, password) VALUES ('".mysqli_real_escape_string($link, $_POST['email'])."',
'".mysqli_real_escape_string($link, $_POST['password'])."')";
mysqli_query($link, $query);
if(!mysqli_query($link, $query))
{
$error = "<p>You could not register, please try again.</p>";
}
you can find my problem: The INSERT INTO... line doesn't work, and I don't know why. Every time when I try to "sign up", it says "You could not register, please try again."
(I changed the informations at $link, so you can't use it.
source https://stackoverflow.com/questions/71285977/why-insert-into-query-not-working-with-mysqli
No comments:
Post a Comment