• This is my php so far and I get the error message: Could not update data: Query was empty. Any suggestions? And also could someone show me how to display what the grade is in the html form. Thanks
    <?php
    if(isset($_POST[‘update’]))
    {
    $dbhost = ‘localhost’;
    $dbuser = ‘qwoif’;
    $dbpass = ‘lhovys’;
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
    die(‘Could not connect: ‘ . mysql_error());
    }

    $user_id = get_current_user_id();

    $grade = $_POST[‘grade’];

    $sql = mysql_query(“UPDATE wp_gsga_users SET grade = $grade WHERE ID = $user_id”);

    mysql_select_db(‘my_db’);
    $retval = mysql_query( $sql, $conn );
    if(! $retval )
    {
    die(‘Could not update data: ‘ . mysql_error());
    }
    echo “Updated data successfully\n”;
    mysql_close($conn);
    }
    else
    {
    ?>
    <form method=”post” action=”<?php $_PHP_SELF ?>”>
    <table width=”400″ border=”0″ cellspacing=”1″ cellpadding=”2″>
    <tr>
    <td width=”100″>Grade</td>
    <td><input name=”grade” type=”text” id=”grade”></td>
    </tr>
    <input name=”update” type=”submit” id=”update” value=”Update”>
    </td>
    </tr>
    </table>
    </form>
    <?php
    }
    ?>

Viewing 3 replies - 1 through 3 (of 3 total)
  • This line is wrong:

    $sql = mysql_query("UPDATE wp_gsga_users SET grade = $grade WHERE ID = $user_id");

    The mysql_query() functionactuall performs the query, it doesn’t just build it and leave it for later on. You need to have that after you connect to the database.

    BUT…

    I’d really suggets not doing it this way unless you need to connecto to a separate database outside of your WordPress installation. If you’re using the same database as the rest of your site, it’s a lot easier, and safer, to use the $wpdb functions that are built in.

    Thread Starter msurana

    (@msurana)

    How would I use the $wpdb functions to connect to my database, show a field to the user, have them edit it, and save

    If you’re using your WordPress database, then the $wpdb object is already connected to the database, so no need to do anything else. If you’re not, you can use your own connection. If you do use your own connection at least use the mysqli_* functions instead of mysql_* as these have been deprecated for a very long time. If you want to do it the right* way, use PDO for your database queries.

    Assuming that you’re using the same database just look at the link I posted above to find out how the $wpdb object works and what it can do.

    As for how to show the field, edit it and save… set up a new admin page and set up your fields and update logic in there.

    * PDO is the suggested database interface for any new programming these days.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘SQL and PHP in wordpress site to gather data for the current user logged in’ is closed to new replies.