WordPress.org

Ready to get started?Download WordPress

Forums

newb with mysql query question (20 posts)

  1. maestro42
    Member
    Posted 6 years ago #

    okay so total newb on the plugin front here, so please be nice ;)

    I am writing a simple plugin for my website. I have a basketball website and want to have a scoreboard on the homepage. However, I want to have all the data on the scoreboard easily changed from inside wordpress.

    I created the plugin and got that working okay. I created the table in mysql and got that working okay. The table is called "scoreboard" and I have 2 rows with data in it for now. However, here is my problem.

    How do i query that table to 1) show the current values on my admin page and 2) update those values as necessary from the admin page? I have been searching all night and I cannot get it to work right.

    Thanks for any help!

  2. maestro42
    Member
    Posted 6 years ago #

    my goal is this:

    I want a scoreboard on the homepage, say with 4 games and scores on it. We do live updating of the scores as the games are going on.

    So i want one person to be able to be logged in a updating Game 1 and have another person be able to be logged in somewhere updating Game 2.

    Any thoughts ?

  3. maestro42
    Member
    Posted 6 years ago #

    any advice on how to get started with something like this?

    Shouldn't be too hard, I don't think.

    Thanks.

  4. maestro42
    Member
    Posted 6 years ago #

    i started to get to be able to query the database. But how can I structure it so that users can go into the admin area and update the scores ?

    Thanks

  5. Michael Torbert
    WordPress Virtuoso
    Posted 6 years ago #

    Use the wpdb class. It works basically the same as any other SQL query.
    http://codex.wordpress.org/wpdb

  6. maestro42
    Member
    Posted 6 years ago #

    okay. Looked through that a bunch.

    Here is my question.

    I need to write a form on my admin page that will populate the fields with the current value in the table, then allow the user to enter a new value and hit an update button, which will update the value in the table, kind of like how you can set your options in the settings pages in the default WP admin area.

    How would I structure that? Also, keeping in mind that I am using a custom added table to my database.

    Thanks

  7. Michael Torbert
    WordPress Virtuoso
    Posted 6 years ago #

    This isn't really a WordPress question. It's more of a PHP/HTML question.

    Use the WordPress database class to retrieve the info from the database. Set the "value" attribute for each field equal to the appropriate value from the database. This will populate the fields in the form. On the submit action, have it gather the field values and use the WordPress database class again to update the database table.

  8. maestro42
    Member
    Posted 6 years ago #

    okay. thanks for your help. I will try to see what i can work out.

  9. Michael Torbert
    WordPress Virtuoso
    Posted 6 years ago #

    No problem. Also, you can download http://wordpress.org/extend/plugins/sms-text-message/ and look at the code. I do this on the plugin options page.

  10. maestro42
    Member
    Posted 6 years ago #

    Okay. Thanks so much. I will take a look at that.

  11. maestro42
    Member
    Posted 6 years ago #

    hey so here is what I have:

    <form name='update_team1' id='update_team1' method='POST' action='<?="http://"
    . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING']
    >'>
    <br />
    <input name="team1" value="<?php echo $team1; ?>" type="text" /><input type="submit" value="Update Widget Footer" /><br />
    </form>

    So when the page displays, the correct team name shows up in input field, however when I change it and hit submit - the change does not take effect ?

    Any ideas ?

  12. Michael Torbert
    WordPress Virtuoso
    Posted 6 years ago #

    Do you have code to receive the data?

  13. maestro42
    Member
    Posted 6 years ago #

    not sure what you mean by this ? So guessing I don't. Can you help me with that ? Thanks!

  14. Michael Torbert
    WordPress Virtuoso
    Posted 6 years ago #

    Again, this really isn't a WordPress question. You need to review PHP form processing.

  15. maestro42
    Member
    Posted 6 years ago #

    okay I think I got it all working except for one little problem. When I go to post the new value into the table using UPDATE, if I manually type in wpSite_scoreboard - it works okay. When I use $wpdb->prefix . "scoreboard" it does not work.

    When I do this $table_name = $wpdb->prefix . "scoreboard"; and then echo $table_name, I only get this printed out : scoreboard, but it should be printing out : wpSite_scoreboard.

    Any reason in general why it would not work correctly. I see how you did it and you just used the same $wpdb-> prefix . "table_name" idea, so is there any file that I must include or something I am missing that would make this not work?

    This is the last piece I need, I think!
    Thanks!

  16. Michael Torbert
    WordPress Virtuoso
    Posted 6 years ago #

    Show me the rest of your code.

  17. maestro42
    Member
    Posted 6 years ago #

    okay. Thank you very much for your help!

    <?php if( $_POST['team1'] != ''){
    
    $team1 = $_POST['team1'];
    $id = $_POST['id'];
    
    $table_name = $wpdb->prefix . "scoreboard";
    $quer = "UPDATE " . $table_name . " SET team1 = '" . $team1 . "' WHERE id = '" . $id . "'";
    
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($quer);
    }
    ?>
    
    <?php
    
    	global $wpdb;
    	$table_name = $wpdb->prefix . "scoreboard";
    	$result = $wpdb->get_results("SELECT * FROM " . $table_name . " WHERE id=1");
    
       foreach ($result as $results) {
          $id = $results->id;
          $team1 = $results->team1;
          $team1_score = $results->team1_score;
          $team2 = $results->team2;
          $team2_score = $results->team2_score;
        }
    ?>
          <form name='update_team1' id='update_team1' method='POST' action='<?= "http://"
             . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'] ?>'>
    
             <input type='hidden' name='id' value="<?php echo $id; ?>" /><input name="team1" value="<?php echo $team1; ?>" type="text" /><input type="submit" value="Update Widget Footer" />
          </form>

    The above code does not update the value in the table when submitted.

    If I change the 7th line from:

    $quer = "UPDATE " . $table_name . " SET team1 = '" . $team1 . "' WHERE id = '" . $id . "'";

    to:

    $quer = "UPDATE wpSite_scoreboard SET team1 = '" . $team1 . "' WHERE id = '" . $id . "'";

    it works okay. Can't figure out why!

  18. Michael Torbert
    WordPress Virtuoso
    Posted 6 years ago #

    Email me. michael (AT) semperfiwebdesign (DOT) com
    It's easier than going back and forth like this.

  19. Michael Torbert
    WordPress Virtuoso
    Posted 6 years ago #

    Nevermind, I looked at it again. You messed up your string. Take a closer look at your 's and "s.

  20. maestro42
    Member
    Posted 6 years ago #

    okay. thanks a lot.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.