Jump to content

Adding random number into DB issue..


iCeR

Recommended Posts

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.

 

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?

 

2. 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?

 

 

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!

Link to comment
Share on other sites

you insert email in 1st try block

in this query

 $mysqli->query("INSERT INTO coming_soon_emails VALUES('email', SUBSTRING(MD5(UUID()),FLOOR(RAND()*25),5), UNIX_TIMESTAMP())");

you try to insert string 'email' not value of variable $email

if you tray to insert variable $email it didn't work to because $email is inserted before in try block

change your 2nd query to UPDATE or 1st to SELECT

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.