New programmer- I have created a function to add a user to a database. This function is found in class USER. I have set up mysql to have user_name as a unique key. If a user tries to enter a name that already exists it is not entered into the mysql database but my form says ok its been submitted and just moves to the next page. I want to let the user know that the name already exists and create an error on the registration form. Is there a way to append that to this function?
function add_member($name, $email, $password)
{
global $mysqli;
$query = "INSERT INTO members
SET
user_name = '".addslashes($name)."',
user_email = '".addslashes($email)."',
password = '". md5($password) ."'";
$success = $mysqli->query ($query);
if (!$success || $mysqli -> affected_rows == 0)
{
echo "<p> An error occurred: you just are not tough enough!!! </p>";
return FALSE;
}
$uid = $mysqli -> insert_id;
return $uid;
if (!$found_error)
{
header("location: homepage.php");
}
In order to prevent the redirect, you have to inform your front end that there’s been an error.
Your form will always attempt to “move to the next page” whenever a user clicks “submit.” It will do whatever action is set.
In that action (which I assume is the next page in the process), you should have the page handle the error – i.e. :
note that using header-redirects is somewhat inelegant, but the best practices of how to handle error is out of scope of this question…
My point is, you must catch the error on your result page, and have that page handle a failed attempt.