It always returns empty connect_error
value. It connects to mySQL properly. I want to create simple registering system on my page. I'm new to PHP and HTML, so I don't know what is wrong in this code. MySQL works properly, I think, so maybe something is wrong with my SQL code?
This is my PHP code:
<?php
$link = @new mysqli('valid localhost', 'valid user', 'valid password', 'valid database');
if ($link->connect_error!=0) {
die('Could not connect: ' . $link->connect_error);
}
$login = $_POST['login'];
$password = $_POST['password'];
$confirm = $_POST['confirmpassword'];
$email = $_POST['email'];
$result = mysql_query($sql, "SELECT COUNT(*) AS num_rows FROM `users` WHERE username='{$login}' LIMIT 1;");
$row = mysql_fetch_array($sql, $result);
if($row["num_rows"] < 0){
header('Location: index.php');
return;
}
if($password != $confirm) {
header('Location: index.php');
return;
}
$login = htmlentities($login, ENT_QUOTES, "UTF-8");
$password = htmlentities($password, ENT_QUOTES, "UTF-8");
$email = htmlentities($email, ENT_QUOTES, "UTF-8");
$sql = sprintf("INSERT INTO `users` (`username`, `password`, `email`) VALUES
('%s', '%s', '%s');",
mysqli_real_escape_string($link, $login),
mysqli_real_escape_string($link, $password),
mysqli_real_escape_string($link, $email));
if (mysql_query($sql, $link)) {
echo "User created successfully!
";
} else {
echo 'Error creating user: ' . $link->connect_error . "
";
}
?>
HTML code:
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>Awesome title</title>
</head>
<body>
Register<br /><br />
<form action="register.php" method="post">
Login: <br /> <input type="text" name="login" /> <br />
Email: <br /> <input type="email" name="email" /> <br />
Password: <br /> <input type="password" name="password" /> <br />
Confirm password: <br /> <input type="password" name="confirmpassword" /> <br /><br />
<input type="submit" value="Register" />
</form>
</body>
</html>
and MySQL table:
CREATE TABLE IF NOT EXISTS `users` (
`username` text collate utf8_polish_ci NOT NULL,
`password` text collate utf8_polish_ci NOT NULL,
`email` text collate utf8_polish_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci;
This is what I get:
Error creating user: