我有这个 PHP 代码:
$sql="SELECT * from project_gallery where sequence = '".$_POST["project_sequence"]."' ";
$rs=mysql_query($sql,$conn);
$project=mysql_fetch_array($rs);
$images = explode(',',$project['images']);
$images = "," . $images . ","; // add comma to beginning and end of list
foreach ($images_to_delete as $image_to_delete)
{
str_replace("," . $image_to_delete . ",", ","); // e.g. replaces ",image1.jpg," with ","
}
$images = substr($images, 1, -1); // remove comma from beginning and end
echo $images;
我希望能够从我的 MySQL 表中的单元格中删除字符串的选定(在以前的 HTML 表单中)部分。
在我的表中,我的图像文件名存储如下:
[image1.jpg],[image2.jpg],[image3.jpg],[image4.jpg]
ETC...
因此,如果选中 image2.jpg 的复选框并提交表单,我想让字符串看起来像:
`[image1.jpg],[image3.jpg],[image4.jpg]`
这也是我的 HTML 表单:
<?php
$sql="SELECT * from project_gallery where sequence = '".$_GET["project"]."' ";
$rs=mysql_query($sql,$conn);
$project=mysql_fetch_array($rs);
$images = explode(',',$project['images']);
?>
<form action="edit.php?project=<?php echo $_GET["project"]; ?>" method="post" enctype="multipart/form-data">
<table width="100%" border="0" cellspacing="5" cellpadding="5">
<tr>
<td><strong>Project Name</strong><br />
<input type="text" name="project_name" value="<?php echo $project["name"]; ?>" style="width:100%;" /></td>
</tr>
<tr>
<td><strong>Add New Images</strong><br /><input type="file" name="images[]" multiple="multiple" /></td>
</tr>
<?php
foreach($images as $image)
{
$display_image = substr($image, 1, -1);
?>
<tr>
<td><img src="/img/project-gallery/<?php echo $display_image; ?>" width="160px" /><br />
<input type="checkbox" name="images_to_delete[]" id="images_to_delete[]" value="<?php echo $display_image; ?>" /></td>
</tr>
<?php
}
?>
<tr>
<td><input type="text" name="project_sequence" id="project_sequence" value="<?php echo $project["sequence"]; ?>" />
<input type="submit" name="submit" id="submit" value="Save Project" /></td>
</tr>
</table>
</form>