Why cannot send just one post comment in PHP?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • notfound
    New Member
    • Jun 2012
    • 28

    Why cannot send just one post comment in PHP?

    I am trying to write a PHP project.In this site people can share photos by logining and the photos are displayed in the index.php that is the main page of the program.Now I have three database users images and messages.I can provide users to share their photos by saved the information about the photos, and then get them from the database from images table.Now I want to provide a user to make a comment about a photo in the main page.There are textarea and post button under the images.But when I make a comment for an image it is registered not only related picture id but also for the other picture ids.Why does this happen?

    Here is the some part of the php code, I also open and close the database connection in the necessary parts.

    Code:
     while($x < mysql_num_rows($result))
            {   
          $picid=mysql_result($result, $x, 'id');
          $name=mysql_result($result, $x, 'imageName');
          $date=mysql_result($result, $x, 'imageDate');
    
            echo "<div><img src = 'upload/$name' height='300 width='300' /></div>";
            echo "Name: $name   Date: $date";
            echo "<br/>";
           ?>
           <p>Post a Comment</p>
    
          <form action="<?= $_SERVER['PHP_SELF']?>" method="POST">
          <textarea name="message" cols="70" rows="6" placeholder="Enter a comment">
    
         </textarea><br/><br/><br/><br/><br/><br/>
          <input type="submit" name="submit" value="Post" style="float:left"><br/><br/>    
          <br/>
          </form>
         <!--<textarea name="message" cols="70" rows="6" ></textarea><br/><br/><br/><br/>   
         <br/><br/>-->
           <?php
    
            extract($_REQUEST);
            if (isset($submit)) {
              $y=0;
              $email=$_SESSION['email'];
               $asdf = mysql_query("SELECT * from users WHERE email='$email'");
               //var_dump(mysql_error());
                while( $y<mysql_num_rows($asdf) )
               { 
                $iduser=mysql_result($asdf,$y,'id');
                 $y++;
              }
    
                   echo "$iduser";
                   $sql ="INSERT INTO messages (id_user, message,picid) VALUES   
                   ('$iduser','$message','$picid');";
    
                   $rslt = mysql_query($sql);
    
                   if ($rslt== false) {
                    echo '<p>Error: cannot execute query</p>';
                   }
    
                  } ?>
                  <?php
    
          $x++;
    
        }
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    One of the reasons is that your insert is inside your loop that you established on line 1.

    Comment

    • notfound
      New Member
      • Jun 2012
      • 28

      #3
      @Rabbit When I try to write out of of the loop it just take the last picid for all forms.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        That's because you're pulling your picid from the loop. You need to use the picid that the user put their comment on.

        Comment

        Working...