我应该能够解决这个问题,但我一直在圈子里。我有一个带复选框的表单。选中复选框后,它们会被标记为删除。
我使用foreach处理这些选中的复选框:
foreach (array_keys($_POST['checkboxselect']) as $k) {
$str .= $k.',';
}
这是我将视频列表放在一起的蹩脚尝试。我正在使用的sql删除是:
DELETE FROM `videos` WHERE `video_name` IN ($str)
所以我在这里遗漏了很多东西。 $str
需要:
感谢您的帮助。
答案 0 :(得分:2)
像这样使用implode()
:
$str = '"' . implode('","', array_keys($_POST['checkboxselect'])) . '"';
implode()
将获取一个数组并使用“glue”字符串连接数组中的每个值。在这种情况下,“胶水”是","
,数组由$_POST['checkboxselect']
中的键组成。最后,生成的字符串包含在"
。
这将产生您想要的示例字符串"myvideo.mp4","myvideo2.mp4","myvideo3.mp4"
。
答案 1 :(得分:2)
尝试使用PHP的implode:
// Init links
$mysqliLink = mysqli_connect('host', 'user', 'password') or die('Could not connect: ' . mysqli_error());
$mysqlLink = mysql_connect('host', 'user', 'password') or die('Could not connect: ' . mysql_error());
//-----
// Prep values for query
//-----
// Only pick one of the following depending upon the mysql library you are using
// If using mysql, passing the 2nd argument is not necessary, but for
// clarity, adding it
$deleteNames = array_map('mysql_real_escape_string', array_keys($_POST['checkboxselect']), array_fill(0, count($_POST['checkboxselect']), $mysqlLink));
// If using mysqli
// This will ensure that $mysqliLink will be passed in as first arg for
// every iteration satisfying the function signature
$deleteNames = array_map('mysqli_real_escape_string', array_fill(0, count($_POST['checkboxselect']), $mysqliLink), array_keys($_POST['checkboxselect']));
//-----
// Because you are passing strings, we need to ensure each is wrapped in quotes
$deleteNames = "'" . implode("','", $deleteNames) . "'";
// Construct query using implode
$sql = sprintf('DELETE FROM `videos` WHERE `video_name` IN (%s)', $deleteNames);
- 更新 -
使用Joomla API:
$db = &JFactory::getDBO();
// Localize and sanitize each individual value
foreach (array_keys($_POST['checkboxselect']) as $element) {
$deleteNames[] = $db->quote($element);
}
// Because you are passing strings, we need to ensure each is wrapped in quotes
// No longer true because $db->quote taking care of quoting out each name for us
$deleteNames = implode(',', $deleteNames);
// Construct query using implode
$sql = sprintf('DELETE FROM `videos` WHERE `video_name` IN (%s)', $deleteNames);