It's because of this
if(isset($_POST['submit'])){
your submit button is called/named Login
and not submit
<input type="submit" value="Invite" name="Login" />
and the conditional statement as well as everything inside it, failed because of it.
Therefore, $email = $_POST['email'];
will not be passed on as the value.
change it to
if(isset($_POST['Login'])){
Add error reporting to the top of your file(s) right after your opening <?php
tag:
error_reporting(E_ALL);
ini_set('display_errors', 1);
which would have signaled any errors found.
"how can I then redirect the user back to invite.php from save.php"
Add a header()
:
I.e.:
header("Location: http://www.yoursite.com/invite.php");
exit;
and make sure you're not outputting before it.
See this article on SO, should that happen:
On an added note, when a user will be redirected to invite.php
or initially visits your page, your code generator will execute, therefore it may be best to wrap everything inside the submit button's conditional statement.
For example:
if(isset($_POST['Login'])){
$email = $_POST['email'];
$length = 10;
$inviteCode = "";
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
for ($p = 0; $p < $length; $p++) {
$inviteCode .= $characters[mt_rand(10, strlen($characters))];
}
} // end brace for if(isset($_POST['Login']))
that would be up to you to decide if that's the way you wish your code to run as.
Edit:
I'm having trouble understanding how/why you're using three pages for all this.
You could set it up this way instead (HTML form, then PHP):
<?php
if(isset($_POST['Login'])){
$email = $_POST['email'];
$length = 10;
$inviteCode = "";
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
for ($p = 0; $p < $length; $p++) {
$inviteCode .= $characters[mt_rand(10, strlen($characters))];
}
// execute SQL codes here
// if query is successful, redirect
// i.e.
// if($query){ header("Location: http://www.yoursite.com/invite.php"); exit; }
} // end brace for if(isset($_POST['Login']))
?>
mysqli_
method example:
Nota: Make sure inviteCode
is the actual column name and not invitecode
. They are case-sensitive.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$DB_HOST = "xxx"; // replace
$DB_NAME = "xxx"; // replace
$DB_USER = "xxx"; // replace
$DB_PASS = "xxx"; // replace
$conn = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($conn->connect_errno > 0) {
die('Connection failed [' . $conn->connect_error . ']');
}
if(isset($_POST['Login'])){
$email = mysqli_real_escape_string($conn,$_POST['email']);
$length = 10;
$inviteCode = "";
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
for ($p = 0; $p < $length; $p++) {
$inviteCode .= $characters[mt_rand(10, strlen($characters))];
}
$query = mysqli_query($conn, "INSERT INTO `referrals` (`email`, `inviteCode`)
VALUES ('$email', '$inviteCode') ");
if($query){
echo "Success";
// or redirect with header, but remove the echo above if using header
}
else{
// echo "Sorry";
die('Error querying database. ' . mysqli_error($conn));
}
} // end brace for if(isset($_POST['Login']))