Search code examples
phpmysqlitextboxwebdata-retrieval

adding or recognizing multiple textboxes or something like that in php dynamically


hi
i am amateur in php and making a website in which i am uploading an image,storing it in database(with an autoincrement variable) and displaying it on page in descending order so it may seem as latest at top and latter below it,,i need to add a textbox for comment and username(cuz i haven't made login page as m not gonna host it somewhere)
i knwo how to add it for upload image's section as only 1 image is gonna get uploaded
problem is how to display it, i have used

while ($image = mysqli_fetch_assoc($sql))
{
echo image code....
}



Now i in the div to display image i'll incorporate 2 textboxes and evertime after while loop ill display values from dB accordingly but when i want to edit the textbox after uploading more than 1 image,how to know which textbox is being edited as every textbox would have same textbox name
should i compare the image contents ?as the image iteself is stored in binary format?
but that wud be a slow process i guess
plz suggest som idea or method to do so
basically its like instagram(but very basic)....


Solution

  • First create images table:


    images table

    image_id | url


    Then creare seperate table for images comments:


    images_comments table

    comment_id | image_id | user | text | date


    Where:

    comment_id: integer , Auto Increment
    image_id: integer referring to id in images table
    user: should be user id referring to separate users table, but because you do not have login system you can store user name here as varchar.
    text: varchar
    date: datetime

    To show images + comments:

    $sql = "SELECT * FROM images";
    
    while ($image = mysqli_fetch_assoc($sql))
    {
       $imageUrl = $image["url"];
       $imageID = $image["image_id"];
    
       //Show image:
       echo "<img src='$imageUrl ' />";
    
       //Get comments:
       $sql2 = "SELECT * FROM images_comments WHERE image_id=$imageID";
       while ($comment = mysqli_fetch_assoc($sql2))
       {
          $text = $comment["text"];
          $userName = $comment["user"];
          $date = $comment["date"];       
          //show username and comment date
          echo $userName . " " .$date . "<br>";
          //Show comment text:
          echo $text; 
       }
    }
    


    To add a comment to images
    if you want a comment form under each image then in the While loop we used before add the following code:

    while ($image = mysqli_fetch_assoc($sql))
    {
        .......... previous code goes here .........
    
        //in the form we store imageID in hidden input field
        //so that we can know to which image the form belongs
        echo "<form method='post' action='page.php'>
                 <input type='text' name='username'/>
                 <textarea name='commentText'></textarea>
                 <input type='hidden' name='imageID' value='$imageID'/>
                 <input type='submit' name='submitComment' value='Submit'>
              </form>";
    
    }
    


    then in page.php

    if(isset($_POST["submitComment"]))
    {
        $username = $_POST["username"];
        $text = $_POST["commentText"];
        $imageID = $_POST["imageID"];
        $date = date("Y-m-d H:i:s");
    
        $sql = "INSERT INTO images_comments (image_id,user,text,date) VALUES
                ($imageID,$username,$text,$date)";
        mysqli_query($con,$sql);
    }