" height="140" width="140" /> " placeholder-6ren">
gpt4 book ai didi

php - 使用从数据库动态创建的输入字段更新 mysql 中的表

转载 作者:太空宇宙 更新时间:2023-11-03 12:21:16 25 4
gpt4 key购买 nike

我想更新数据库中的字段。这是我从数据库中获取记录并以表格形式显示它们的代码。

<form name="create_album" method="POST" action="" style="width:auto;">

<label for="First Name">Album Name</label>

<input type="text" name="album" id="album" placeholder="Album Name"value="<?php echo $album_name; ?>" />


<?php
$result = mysql_query("select * from user_uploads where album = '$album_name' ");
while($row=mysql_fetch_array($result))
{
?>
<img src="<?php echo $row['image_name']; ?>" height="140" width="140" />
<input type="text" name="des[]" value="<?php echo $row['image_description']; ?>" placeholder="Image Description Here"/>
<input type="text" name="id[]" value="<?php echo $row['id']; ?>" placeholder="Image Description Here"/>

<?php } ?>
<input type="submit" name="submit" value="Upload Album"/>

但是现在当我想更新数据库中的记录时,它会使用最后一个输入字段的值将所有记录更新到表中。这是 PHP 代码。

  <?php 
if(IsSet($_POST["submit"]))
{
$album = $_POST["album"];
$des=$_POST["des"];
foreach($_POST['id'] as $id)
{

$update_qry = "update user_uploads set album='$album', image_description='$des' where id = '$id' ";

$result_update_image = mysql_query($update_qry);
if (!$result_update_image)
{
header("location:create_album.php?errmsg=Album Not Updated");
}



}

}
}
?>

但是我不知道如何根据id获取des form数组的所有值在表中更新。

最佳答案

像这样更改代码并检查。

   $i=0;
$des=$_POST["des"];
foreach($_POST['id'] as $id)
{

$update_qry = "update user_uploads set album='".$album."', image_description='".$des[$i]."' where id = '".$id."' ";

$result_update_image = mysql_query($update_qry);
$i++;
}

关于php - 使用从数据库动态创建的输入字段更新 mysql 中的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19901995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com