自包含的示例...
<html>
<head><title>...</title></head>
<body>
<form method="post" action="?">
<p>
<!-- php will parse post fields like check_box[]=xyz as an array, appending new elements to $_POST['check_box'] -->
<input type="checkbox" name="check_box[]" id="c1" value="140" /><label for="c1">140</label><br />
<input type="checkbox" name="check_box[]" id="c2" value="141" /><label for="c2">141</label><br />
<input type="checkbox" name="check_box[]" id="c3" value="142" /><label for="c3">142</label><br />
<input type="checkbox" name="check_box[]" id="c4" value="143" /><label for="c4">143</label><br />
<input type="checkbox" name="check_box[]" id="c5" value="144" /><label for="c5">144</label><br />
<input type="submit" />
</p>
</form>
<?php
if (isset($_POST['check_box']) && is_array($_POST['check_box'])) {
echo '<pre> _POST='; var_dump($_POST); echo '<pre>';
// approach #1: treat ids as numbers. Keep the value range of php's integers and MySQL numeric fields in mind
// make sure the elements really are integers
$params = array_map('intval', $_POST['check_box']);
// join the elements to one string like "1,2,3"
$params = join(', ', $params);
// use the IN operator in your WHERE-clause
$sql = "DELETE FROM xyz WHERE cat_id IN ($params)";
echo 'sql1 = ', $sql, "<br />";
// mysql_query($sql, $mysql) or die(mysql_error());
// approach #2: treat ids as strings.
// you need a database connection for mysql_real_escape_string()
$mysql = mysql_connect('localhost', 'localonly', 'localonly') or die(mysql_error());
mysql_select_db('test', $mysql) or die(mysql_error());
// each element has to be escaped and put into single quotes
$params = array_map(
function($e) use ($mysql) {
return "'".mysql_real_escape_string($e, $mysql)."'";
},
$_POST['check_box']
);
// again join them, "'1','2','x'"
$params = join(', ', $params);
// IN operator in WHERE-clause
$sql = "DELETE FROM xyz WHERE cat_id IN ($params)";
echo 'sql2 = ', $sql, "<br />";
// mysql_query($sql, $mysql) or die(mysql_error());
}
?>
</body>
</html>
這是因爲您的「check_box」變量e只有第一個選定的check_box作爲值。我不太瞭解PHP,但如果這確實是PHP,那麼答案就在那裏。 – MPelletier 2010-07-31 15:48:34
建議將所有傳入的check_box值轉換爲「IN()」。任何答案與該解決方案將得到我的投票。 – 2010-07-31 15:49:35
您對POST字段的名稱有影響嗎?即你可以讓他們例如'check_box []'而不是'checkbox'? PHP會將前者解析爲數組,而後者僅存儲最後一個值。 – VolkerK 2010-07-31 15:54:53