this the php part
<?php
include 'assets/db.php';
if(isset($_POST["register"])){
$sql = "INSERT INTO table (name, email, user_name, pass)
VALUES ('".mysqli_real_escape_string($conn, $_POST["name"])."',
'".mysqli_real_escape_string($conn, $_POST["email"])."',
'".mysqli_real_escape_string($conn, $_POST["user_name"])."',
'".mysqli_real_escape_string($conn, $_POST["pass"])."')";
if ($conn->query($sql) === TRUE) {
echo "<script type='text/javascript'>window.top.location='_sign_in.php?l=1';</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error! Please refresh the page and try again.');</script>";
}
$conn->close();
i am trying to avoid re-registration of any user_name, if user_name is already registered, redirect back to some page example- register.php?q=user_exists.
To avoid duplicate user names, add a unique index to the table:
ALTER TABLE `table` ADD UNIQUE(`user_name`)
This will cause the insert to fail allowing you to detect the failure without needing an additional SELECT
to check for a duplicate.
If you want to ensure the error is due to a duplicate value, check for mysql error 1062
Error: 1062 SQLSTATE: 23000 (ER_DUP_ENTRY)
You can check for this by using mysqli::errno and then redirect by simply sending the Location header, ie:
if (!$conn->query($sql))
{
if ($conn->errno == 1062)
{
header("Location: register.php?q=user_exists");
die();
}
die('Critical Failure');
}
Edit: Instead of just down voting, how about commenting on what is wrong with this answer?
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments