I have 3 columns in my DB: email (primary), unique_code, timestamp
Users enter their email address and it is added to the DB, including an alphanumeric 5 digit unique code in the ‘unique_code’ column and a timestamp in the ‘timestamp’ column.
[COLOR=“DarkRed”]1. The email address is being added, but not the unique code? What is the issue here and what can I should I specifically do to fix it?
- The next thing is that I need to display that code to the user in where it says <?php echo $unique_code;?>. How can I do that?[/COLOR]
DB is as follows:
Field Type Collation Attributes Null Default Extra Action
email varchar(64) utf8_unicode_ci No None
unique_code varchar(64) utf8_unicode_ci No None
timestamp timestamp on update CURRENT_TIMESTAMP No CURRENT_TIMESTAMP
INDEXES
PRIMARY BTREE Yes No email 7 A
Full code
<?php
require "includes/connect.php";
$msg = '';
if($_POST['email']){
// Requested with AJAX:
$ajax = ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
try{
if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){
throw new Exception('Invalid Email!');
}
$mysqli->query("INSERT INTO coming_soon_emails
SET email='".$mysqli->real_escape_string($_POST['email'])."'");
if($mysqli->affected_rows != 1){
throw new Exception('This email already exists in the database.');
}
if($ajax){
die('{"status":1}');
}
$msg = "Thank you!";
$mysqli->query("INSERT INTO coming_soon_emails VALUES('email', SUBSTRING(MD5(UUID()),FLOOR(RAND()*25),5), UNIX_TIMESTAMP())");
echo "Something went wrong:" . $mysqli->error;
}
catch (Exception $e){
if($ajax){
die(json_encode(array('error'=>$e->getMessage())));
}
$msg = $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>example</title>
<link rel="stylesheet" type="text/css" href="css/styles.css" />
</head>
<body>
<div id="container">
<form id="form" method="post" action="">
<input type="text" id="email" name="email" value="<?php echo $msg?>" />
<input type="submit" value="Submit" id="submitButton" />
</form>
<div id="thankyou">
Thank you! <?php echo $unique_code;?></p>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script src="js/script.js"></script>
</body>
</html>
Thank you!