(Please bear with me.) I want to redirect to a page if this error is produced. If there is a duplicate entry in the database (i.e. If something goes wrong after submission to db but the user resubmits the page.)
Error: INSERT INTO di_ssenisub (timestamp, business_name, business_type, username, main_email, password) VALUES (NOW(), '123', 'Hotel', '123', 'jkljllk@mrmd.com', '$2y$10$gYxXbmHKNmqrEHhB2qOw8uVVs7HNOmdN4oTpv8m98.lN/3R6MwseG') Duplicate entry '123' for key ‘PRIMARY’
My code currently is set on this else:
else {
echo "Error: " . $insertSQL . "<br>" . mysqli_error($link);
}
What I want to do is redirect them to a log in/error page, so they can log in and finish the registration from the admin panel.
Any thoughts? Thanks.
$insertSQL = sprintf("INSERT INTO di_ssenisub (timestamp,
business_name,
business_type,
username,
main_email,
password)
VALUES (NOW(), %s, %s, %s, %s, %s)",
GetSQLValueString($business_name, "text"),
GetSQLValueString($business_type, "text"),
GetSQLValueString($_POST['username_entry'], "text"),
GetSQLValueString($main_email, "text"),
GetSQLValueString($hashPass, "text"));
if (mysqli_query($link, $insertSQL)) {
session_start();
$_SESSION['username'] = $username_check;
$_SESSION['business_name'] = $business_name;
$_SESSION['business_type'] = $business_type;
header('Location: ../../register/' .$business_type_link);
}
"UPDATE $table_name SET
business_name='$business_name',
business_type='$business_type',
username=$username_entry,
main_email='$main_email',
password = '$hashPass'
WHERE id='$user_id' LIMIT 1");
mysqli_close($link);
$insertSQL = sprintf("INSERT INTO di_ssenisub (timestamp,
business_name,
business_type,
username,
main_email,
password)
VALUES (NOW(), %s, %s, %s, %s, %s)",
GetSQLValueString($business_name, "text"),
GetSQLValueString($business_type, "text"),
GetSQLValueString($_POST['username_entry'], "text"),
GetSQLValueString($main_email, "text"),
GetSQLValueString($hashPass, "text"));
$affected += mysqli_rows_affected($link);
$insert_error = mysqli_errorno($link);
"UPDATE $table_name SET
business_name='$business_name',
business_type='$business_type',
username=$username_entry,
main_email='$main_email',
password = '$hashPass'
WHERE id='$user_id' LIMIT 1");
$affected += mysqli_rows_affected($link);
$insert_error = mysqli_errorno($link);
session_start();
$_SESSION['username'] = $username_check;
$_SESSION['business_name'] = $business_name;
$_SESSION['business_type'] = $business_type;
}
mysqli_close($link);
After the INSERT and UPDATE, then check the data if the is a problem.
And do the users a favor, Do not reject their user name. Allow duplicate user names.
If you want to use unique user names and force the user to come up with a unique name, add some analytics to see how many abandon your site rather than continue to sign up.
If a unique user name must be used like here on stackoverflow, then have a user name for log in and an on screen name. stackoverflow uses email for log in.
if ($affected == 0){
// do error detection / correction here
// create warnings
include('form.php');
exit
}
else{
include('next_page.php');
}
end of update
Use an include, and repopulate the INPUTS.
To eliminate the duplicate you first do the INSERT and immediately follow the INSERT with an UPDATE.
INSERT INTO di_ssenisub (timestamp, business_name, business_type, username, main_email, password)
No need to check for an error it is faster to just do the update. It is very quick when the record exists.
UPDATE `di_ssenisub` SET `business_name` = '123', `business_type`,...
WHERE `username` = '123';
The WHERE term uses the columns in the PRIMARY Index. The rest go in the SET.
If you want to validate the values if you find a problem DO NOT do a Redirect (`header("Location: ')
You want to pass the problems found on to the user.
Let's say you find a problem like invalid email:
$warn_email = '*";
include('form.php'); exit;
In form.php
$warn_email<inpupt name="email"
Repopulate the INPUTS
You have the submitted data:
$timestamp = $_POST['timestamp'];
$business_name = $_POST['business_name'];
$business_type = $_POST['business_type'];
$username = $_POST['username'];
$main_email = $_POST['main_email'];
With an include these values will automatically be passed to the form.
<input type="text" name="business_name" value="$business_name" />
<input type="text" name="business_type" value="$business_type" />
<input type="text" name="username" value="$username" />
<input type="text" name="main_email" value="$main_email" />
If they are going in Fresh, then the variables are empty, and you get value=""
in your HTML.
If you want to check for an INSERT and OR UPDATE error do this:
$affected = 0;
INSERT ...
$affected += mysqli_rows_affected($link);
UPDATE ...
$affected += mysqli_rows_affected($link);
if ($affected == 0){echo 'Houston we have a problem';}
What is it you want to know, how to detect the error or how to redirect? If you want to redirect to another page, then the answer by holpducki is correct:
header("Location:http://www.example.com/login.php");
exit;
If you are trying to detect an error, then you can use either the mysql_errno()
or mysql_error()
PHP functions:
http://php.net/manual/en/function.mysql-error.php
http://php.net/manual/en/function.mysql-errno.php
For example:
if (mysql_errno($resource) == 1092) {
header("Location:http://www.example.com/login.php"); // send to login
exit;
} else {
...
}
List of MySQL error numbers can be found here:
http://dev.mysql.com/doc/refman/5.6/en/error-messages-server.html
And here:
http://dev.mysql.com/doc/refman/5.6/en/error-messages-client.html