2010-07-31 64 views
2

我試圖把這個,MYSQL刪除其中value =多個POST值

check_box=147&check_box=148&check_box=146 etc.. 

$delete_selected = $connection->real_escape_string($_POST['check_box']) 

$sql = "DELETE FROM categories WHERE cat_id = '$delete_selected'"; 

,但它只是刪除第一check_box值。有沒有辦法循環所有的值?

+1

這是因爲您的「check_box」變量e只有第一個選定的check_box作爲值。我不太瞭解PHP,但如果這確實是PHP,那麼答案就在那裏。 – MPelletier 2010-07-31 15:48:34

+0

建議將所有傳入的check_box值轉換爲「IN()」。任何答案與該解決方案將得到我的投票。 – 2010-07-31 15:49:35

+0

您對POST字段的名稱有影響嗎?即你可以讓他們例如'check_box []'而不是'checkbox'? PHP會將前者解析爲數組,而後者僅存儲最後一個值。 – VolkerK 2010-07-31 15:54:53

回答

6

你需要改變你的POST變量有[],即checkbox[]=。一旦這是固定的,到後端...

考慮您neeed逃跑的每一個值,做這樣的事情:

$clean_values = array(); 
foreach($_POST['check_box'] as $value){ 
    $clean_values[] = $connection->real_escape_string($value); 
} 

$sql = 'DELETE FROM categories WHERE cat_id in ('.implode(',',$clean_values).')'; 

BONUS PHP5.3答:

array_walk($_POST['check_box'],function(&item) use($connection){ 
    $item = $connection->real_escape_string($item); 
}); 
$sql = 'DELETE FROM categories WHERE cat_id in ('.implode(',',$_POST['check_box']).')'; 
+0

+1:'implode'在這裏執行這項工作會減少代碼! – 2010-07-31 16:04:12

+0

謝謝邁克!偉大的代碼! – vii 2010-07-31 16:30:40

2

嘗試

$sql = "DELETE FROM categories WHERE cat_id in '($delete_selected)'"; 

我可能搞砸了PHP。生成的查詢應該liik像

DELETE FROM categories WHERE cat_id in ('cat1', 'cat2', ...) 
+0

+1:你說得對,但是OP還需要檢查PHP以獲取這些值來填充IN子句。 – 2010-07-31 15:51:35

1

自包含的示例...

<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> 
+0

感謝您的詳細示例!非常豐富 – vii 2010-07-31 16:32:29