WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Saving data to database (6 posts)

  1. yadigit
    Member
    Posted 2 years ago #

    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="http://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!

  2. jnhghy
    Member
    Posted 2 years ago #

    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

  3. Digital Raindrops
    Member
    Posted 2 years ago #

    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

  4. jnhghy
    Member
    Posted 2 years ago #

    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

  5. Digital Raindrops
    Member
    Posted 2 years ago #

    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

  6. yadigit
    Member
    Posted 2 years ago #

    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="http://example.com/promo" method="post">
    Promo Code: <input type="text" name="promo" />
    <input type="submit" />
    </form>
    <?php endif; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.