我有这个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]
等...
因此,如果选中 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>
您可以使用 PHP 字符串替换(或类似的东西)来创建新字符串
$newList = str_replace($selectedEntry, '', $imageList);
然后,您可以运行 mysql UPDATE 命令来更新数据库中的该记录。
$sql = "UPDATE table_name SET column_name = '$newList' WHERE id = $id";
也就是说,你可能不应该做你正在做的事情。 您不希望通常必须像这样操作记录。 我建议使用另一个包含所有图像数据(imageID,imageName等)的表,然后在该表中插入和删除整行。 有更有效/更复杂的做事方式,但是如果您最终不得不通过数据库中的字符串/文本/varchar 字段创建列表,您可能没有做正确的事情。
编辑:获取以前检查的条目:
$(function () {
$('#target').click(function () {
var checkValues = $('input[name=checkboxlist]:checked').map(function() {
return $(this).parent().text();
}).get();
//do something with your checkValues array
});
});
你做得太难了。使用 PHP 的内置函数,例如 array_diff(),可以使代码更加简洁,并且(通常)性能更好:
示例:我假设$project是从查询结果中提取的行,而 $images_to_delete 是一个数组。结果是一个字符串,其中包含要保留的新图像列表。
我还展示了PDO的用法,它比不推荐使用的ext/mysql函数更安全,更容易。
$sql="SELECT * from project_gallery where sequence = ?";
$stmt = $pdo->prepare($sql)
or trigger_error($pdo->error, E_USER_ERROR);
$stmt->execute(array($_POST["project_sequence"]))
or trigger_error($pdo->error, E_USER_ERROR);
while ($project = $stmt->fetch(PDO::FETCH_ASSOC) { }
$images = explode(",", $project["images"]);
$images = array_diff($images, $images_to_delete);
$images = implode(",", $images);
echo $images . "'n";
然后删除其余代码:
$images = ",". $images .'",'";将逗号添加到列表
的开头和结尾 foreach ($images_to_delete as $image_to_delete)
{ str_replace("," . $image_to_delete .'",'", '",'");例如,将",image1.jpg"替换为","
} $images = substr($images, 1, -1);删除开头和结尾
的逗号 回声$images;