Adding random number into DB issue..

Discussion in 'PHP' started by SuPrAiCeR69, Mar 15, 2011.

  1. #1
    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>
    PHP:
    Thank you!
     
    SuPrAiCeR69, Mar 15, 2011 IP
  2. joy1986joy

    joy1986joy Member

    Messages:
    189
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #2
    Use a select query just after the insert is done. You select the unique code by the email. because I have seen that you are not entering duplicate emails. or you can store the unique value to a variable first then you insert the variable into the table and echo the variable later for the user. Hope it will help you.
     
    joy1986joy, Mar 16, 2011 IP
  3. SuPrAiCeR69

    SuPrAiCeR69 Peon

    Messages:
    216
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    code example please?
    also creating the uniqe alphanumeric code?
     
    SuPrAiCeR69, Mar 16, 2011 IP
  4. joy1986joy

    joy1986joy Member

    Messages:
    189
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #4
    
    <?php
    
    require "includes/connect.php";
    
    $msg = '';
    //code by JOY
    $email=$_POST['email'];
    //code end
    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();        
        }
    }
    //code by JOY
    $query="select uniquecode form coming_soon_emails where email='$email'";
    $result=mysql_query($query);
    //echo the result to the user
    //code end
    ?>
    
    PHP:
    check the "//code by JOY" blocks.
     
    joy1986joy, Mar 16, 2011 IP
  5. SuPrAiCeR69

    SuPrAiCeR69 Peon

    Messages:
    216
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks! Just an issue before I test..

    Warning: mysql_query() [function.mysql-query]: Access denied for user 'user'@'localhost' (using password: NO) in .../index.php on line 45

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in .../index.php on line 45

    line 44 $query="select uniquecode form coming_soon_emails where email='$email'";
    line 45 $result=mysql_query($query);
     
    SuPrAiCeR69, Mar 16, 2011 IP
  6. joy1986joy

    joy1986joy Member

    Messages:
    189
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #6
    Dear it is your connection problem in your connect.php file. You are giving wrong user name or password. That is why it can not establish the connection. Check it.
     
    joy1986joy, Mar 16, 2011 IP
  7. SuPrAiCeR69

    SuPrAiCeR69 Peon

    Messages:
    216
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    when I use the tutorial code, it connects to the db just fine. the credentials are correct :)
     
    SuPrAiCeR69, Mar 16, 2011 IP
  8. joy1986joy

    joy1986joy Member

    Messages:
    189
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #8
    tell me what is the uniquecode field name and email field name in your "coming_soon_emails" table?
     
    joy1986joy, Mar 16, 2011 IP