• Resolved yadigit

    (@yadigit)


    Hello. I’ve been at this for 4 hours or so. I’ve made good progress, except for this one part.

    The idea of this is to allow my members to type in a text input of let’s say promo, then if the word is correct it will create a random key and send it to a row under the table of wp_users.
    Here’s my Code.

    <?php
    // values for answer trial = input answer = answer
    $trial = $_POST["promokey"];
    $answer = "membersonly";
    if( $trial == $answer) {
    
    // makes code if correct
    function make_seed()
    {
      list($usec, $sec) = explode(' ', microtime());
      return (float) $sec + ((float) $usec * 225); //how long the code is
    }
    mt_srand(make_seed());
    $promocode = mt_rand(); //promocode is code value
    
    //sends promocode to database
    
    wp_insert_user( array ('ID' => $user_id, 'promo_code' => $promocode) ) ;
    
    }else{
    
    <form action="https://mysite.come/promo" method="post">
    Promo Code: <input type="text" name="promokey" />
    <input type="submit" />
    </form>
    <?php } ?>

    I just cant seem to make the promocode enter into the database under wp_users -> ID -> promocode.

    Any Ideas?
    Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi,
    At a first look I can’t find the declaration of $wpdb, to be able to interact with the database throw wordpress functions you need to have $wpdb globalized so you can add as first line of your function :
    `global $wpdb;’
    That should work
    Regards

    Hi,
    This post might be a better way to do it, it is an old post but looks like it will give you a start.

    It adds a field to the users profile and saves it as metadata.

    Set the Meta data:

    if( $_POST["promokey"]=='membersonly' ) {
       /* Create the Promo Code Here */
       function make_seed(){
         list($usec, $sec) = explode(' ', microtime());
         return (float) $sec + ((float) $usec * 225);
       }
       mt_srand(make_seed());
       $promo_code = mt_rand();
    
       /* Save the Promo Code here */
       update_user_meta( $user_id, 'promo_code', $promo_code );
    }

    Get the meta data

    $promo_code = the_author_meta( 'promo_code', $user_id );

    Edited: After Reply Below

    HTH

    David

    Thank for joining in :),
    The function seems to be deprecated I saw that instead you can use update_user_meta
    <?php update_user_meta( $user_id, $meta_key, $meta_value, $prev_value ) ?>
    And it seems to be the best to be used, (I first thought it will only update data without adding it if the row didn’t existed but it clearly specifies in the codex that a new entry will be crated if there isn’t one to be updated)
    Thanks again
    Regards

    Great, I have updated the example above, interesting topic, is this to prove that the user has visited the website and generated a ‘Promo Code’, how is it then used, it could be used as a download check I expect?

    Please mark this topic as resolved!

    David

    Thread Starter yadigit

    (@yadigit)

    Thank you all for the responses.
    I didn’t see these post until now. But I created a way to send it to the database under a different table.
    Here’s my code below.

    <?php
    // values for answer trial = input answer = answer
    $trial = $_POST["promo"];
    $answer = "membersonly";
    if( $trial == $answer) : ?>
    <?php
    // makes code if correct
    function make_seed()
    {
      list($usec, $sec) = explode(' ', microtime());
      return (float) $sec + ((float) $usec * 225); //how long the code is
    }
    mt_srand(make_seed());
    $promocode = mt_rand(); //promocode is code value
    
    //sets USERID to loggedin user is
    $myname = $current_user->user_login;
    $checkname = $current_user->user_login;
    ?>
    <?php
    
     // Collects data from "promocode" table
     $data = mysql_query("SELECT * FROM promocode")
     or die(mysql_error()); 
    
     // puts the "promocode" info into the $info array
     $info = mysql_fetch_array( $data ); 
    
     // IF command to check if data is there.
    if ($checkname = $info['ID']) { ?>
    <?php echo 'Sorry One Promo Code per-person'; ?>
    <?php }else{ ?>
    <?php echo 'Putting data into database';
    
    $wpdb->insert( 'promocode' , array( 'CODE' => $promocode , 'ID' => $myname ), array( '%s', '%s' ) );
    
    }
     ?>
    
    <?php else : ?>
    
    <form action="https://example.com/promo" method="post">
    Promo Code: <input type="text" name="promo" />
    <input type="submit" />
    </form>
    <?php endif; ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Saving data to database’ is closed to new replies.