在php中动态添加或识别多个文本框或类似的内容


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

hi
我是php的业余爱好者,我正在制作一个网站,在其中上传图像,将其存储在数据库中(使用自动增量变量),并按降序显示在页面上,这样它在顶部和底部看起来都是最新的,我需要添加一个文本框,用于注释和用户名(因为我还没有创建登录页面,因为我不会在某个地方托管它)
我知道如何将其添加到上传图像的部分,因为只有1个图像将被上传
问题是如何显示,我用过

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



现在我在div中显示图像,我将合并2个文本框,之后每次while循环都将相应地显示dB的值,但当我在上传多个图像后想要编辑文本框时,如何知道正在编辑哪个文本框,因为每个文本框都有相同的文本框名称
我应该比较图像内容吗?因为图像iteself是以二进制格式存储的
但我想这将是一个缓慢的过程
plz建议som的想法或方法这样做
基本上就像instagram(但非常基本)。。。。

首先创建图像表:


图像表格

image_id|url


然后为图像创建单独的表格注释:


images_comments表格

comment_id|image_id|user|text|date


其中:

comment_id:整数,自动增量
image_id:引用图像表中id的整数
user:应该是引用单独的users表的user-id,但因为您没有登录系统,所以可以将用户名存储为varchar
text:varchar
日期:日期时间

显示图片+评论:

$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; 
   }
}


向图像添加评论
如果你想在每个图像下都有一个评论表单,那么在我们之前使用的While循环中添加以下代码:

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>";
}


然后在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);
}