Need help for a specific MySQL query used in a PHP loop

The main code which i use is

while ($row = mysql_fetch_assoc($result1)) {
    echo $row["user_id"];
    $new    = "[grade uid="{$row["user_id"]}" value="{$groupgrade}" format="{$groupformat}" prv_comment="{$groupprivatecomment}" pub_comment="{$grouppubliccomment}"] ";
    $sqq    = "UPDATE wp_postmeta SET meta_value='$new' WHERE meta_key='grade' AND post_id= $post_id ";
    $result = mysql_query($sqq);
    echo "grade has been updated";
}

Which affects the rows in table wp_postmeta as shown in the picture on the link http://i.stack.imgur.com/gkUiz.jpg Where all the variables undefined here are defined before this para .

Read More

What i actually want that user id must be different in different rows.
The only thing happening in this loop is that each uid from the array gets written in all the rows one after the other and only the last uid persists finally which is 3 in the diagram but i only want that each uid should come separately i.e., differnt uid for diff. meta_id . Please help

REGARDING CODE
$result1 fetches an array of userids from a defined mysql function and rest variables are user defined.
I am kinda new to php and to programming so please help

Related posts

Leave a Reply

3 comments

  1. The question is not very clear and it’s WordPress related and you didn’t add WordPress tag in your question. Anyways, WordPress has it’s own function to handle post meta. In your loop you are trying to update post meta manually but it’s possible to do that using WordPress’ update_post_meta function. The syntax is given bellow

    <?php update_post_meta($post_id, $meta_key, $meta_value, $prev_value); ?>
    

    In this case you can use following code

    <?php update_post_meta($post_id, 'grade', $new); ?>
    

    Here $post_id will be your post id which you want to update.

  2. OK, now that I understand your problem (you confused me by saying that “all rows” were being updated, you just mean all rows with this post_id), I think this is the answer:

    $sql2 = "SELECT meta_id FROM wp_postmeta where meta_key='grade' AND post_id = $post_id";
    $result2 = mysql_query($sql2);
    
    while (($row = mysql_fetch_assoc($result1)) && ($row2 = mysql_fetch_assoc($result2))) {
        echo $row2["meta_id"], ' => ', $row["user_id"];
        $new    = "[grade uid="{$row["user_id"]}" value="{$groupgrade}" format="{$groupformat}" prv_comment="{$groupprivatecomment}" pub_comment="{$grouppubliccomment}"] ";
        $sqq    = "UPDATE wp_postmeta SET meta_value='$new' WHERE meta_key='grade' AND post_id= $post_id AND meta_id = {$row2["meta_id"]}";
        $result = mysql_query($sqq);
        echo " grade has been updated";
    }
    

    The new $sql2 query gets each meta_id, and updates each row with a different user_id from $result11.

  3. your code is to work around this $row["user_id"]

    • if it same user who posted then the result will be true 3
    • if this variable is from other query then u must look to this query how u getting this user_id or , publish it in your question , to see why it comes only 3.

    • its better to make a column for user_id to insert data specialy for that user.

    • $row["user_id"] is beeing a warning variable to sql injection , so it must be escaped by mysql_real_escape_string()