2012-04-15 77 views
2

我有一個表命名related_products,刪除行時先前選定的複選框取消

哪裏 ​​是主要產品。

related_products_ids由與主要產品相關的產品ID組成。

-------------------------------------------- 
| products_id | related_products_ids | 
| ----------------------------------------- 
| 1   | 2     | 
| ----------------------------------------- 
| 1   | 4     | 
| ----------------------------------------- 
| 1   | 3     | 
------------------------------------------- 

我有複選框,

<input value="2" type="checkbox" name="rp_product[]" id="in-category2"> Microsoft IntelliMouse Pro 2 
<input value="3" type="checkbox" name="rp_product[]" id="in-category3"> Microsoft IntelliMouse Pro 3 
<input value="4" type="checkbox" name="rp_product[]" id="in-category3"> Microsoft IntelliMouse Pro 4 

複選框被由PHP生成,

<?php 
echo '<ul id="categorychecklist" class="list:category categorychecklist form-no-clear">'; 
    global $wpdb; 
    $sql = "SELECT related_products_ids FROM ".TABLE_RELATED_PRODUCTS." where products_id = '" . (int)$HTTP_GET_VARS['pID']."'"; 
    $result = mysql_query($sql); 
    $rps = array(); 
    while($row = mysql_fetch_assoc($result)) { 
    $rps[]=$row["related_products_ids"];  
    } 
$rp_sql = "select products_id, products_name from ".TABLE_PRODUCTS_DESCRIPTION." where products_id !='" . (int)$HTTP_GET_VARS['pID']."' order by products_name"; 
    $rp_1 = mysql_query($rp_sql); 
    while($rp_2 = mysql_fetch_array($rp_1)) { 
    $checked = ''; 
    if (in_array($rp_2['products_id'], $rps)) $checked = " checked"; 
    echo "<li id=\"category-".$rp_2['products_id']."\" class=\"popular-category\"><label class=\"selectit\"><input value=\"".$rp_2['products_id']."\" type=\"checkbox\" name=\"rp_product[]\" class=\"rp_item\"" . $checked . "> <span>".$rp_2['products_name']."</span></label></li>"; 
    } 
mysql_free_result($rp_1); 
echo '</ul></div></div>'; 
?> 

我用這個代碼將選中的複選框保存到related_products表,

for ($i=0; $i<count($_POST['rp_product']); $i++) 
{ 
    $check_val1 .= $_POST['rp_product'][$i] .","; //gather data 
} 
$check_val = trim($check_val1, ','); //clean last , 
unset($check_val1); //flush 

$insert_rp_ids1 = explode(',', $check_val); 

foreach($insert_rp_ids1 as $id){ 
    $rps_each2 = array('products_id' => $products_id, 'related_products_ids' => $id); 
    $wpdb->insert(TABLE_RELATED_PRODUCTS, $rps_each2); 
} 

我要的是刪除下related_products_ids列的特定ID時之前選擇的複選框取消..

比方說IM編輯帖子裏的products_id是1並在它的related_products_ids2, 3 and 4 ..我取消了複選框的值爲3(位於related_products_ids),並且在點擊保存按鈕後,related_products_ids上的值3將被刪除,因此當前行數爲2 and 4。怎麼做?我無法想到併爲此找到解決方案。

請幫忙。

回答

0

這裏的邏輯是,在編輯操作中,所有相關產品都會在數據庫中重新填充。如果它們被重新填充,那麼最簡單的方法就是先刪除它們。

爲了達到這個目的,在提交之前,就在你插入任何東西到related_products表之前,刪除​​等於$products_id的所有行。爲此在你的代碼中兩行之間:

$insert_rp_ids1 = explode(',', $check_val); 

foreach($insert_rp_ids1 as $id){ 

此外,還可以考慮使用事務,或者,如果沒有你的表支持,檢查是否delete語句成功。

+0

我做到了,但它也沒有工作.. – Ken 2012-04-16 07:59:12

+0

@Ken你能否確認你的腳本目前能夠在'related_products_ids'表中添加新條目?你只想_remove_取消選擇的條目或編輯你想要添加和刪除的帖子嗎? – bostaf 2012-04-16 08:13:51

+0

對不起。我發現了問題..它對我的SQL刪除語句..現在它的工作..謝謝。 – Ken 2012-04-16 08:26:05

1
<script type="text/javascript"> 
var home = "http://www.mysite.com/"; 
function performAction(url) { 
    document.getElementById("actionFrame").src = (home+url) 
} 
</script> 

<iframe id="actionFrame" width=1 height=1 frameborder=0> </iframe> 

<input type="checkbox" onClick="javascript:performAction('index.php?check=1&action=del');" /> Delete row 
<input type="checkbox" onClick="javascript:performAction('index.php?check=2&action=del');" /> Delete row 

所有的變量應該用PHP編寫的網頁時寫例如:

for ($i=0; $i<sizeof($some_array); $i++) { 
    $input = "<input type=\"checkbox\"" 
      . "onClick=\"javascript:performAction('index.php?check=".($i+1)."&action=del');\" />"; 
    echo $input; 
} 

PHP頁面都會有這樣的事情來執行刪除操作:

$action = $_GET["action"]; 
$check = $_GET["check"]; 

switch ($action) { 
    case "del": 
     delRow($check); 
     break; 
} 

require_once("database.php"); #file which creates the PDO object $db 

function delRow($check) { 
    global $db; //because $db is out of scope until we do this 
    $sql = "DELETE FROM tbl_name WHERE col_id='?';"; 
    $query = $db->prepare($sql); 
    $query->bindParam((int)$check); 
    $query->execute(); 
    $rows = $query->rowCount(); 
    echo "$rows were deleted."; 
} 
+0

那不是我想要的..我簡單地想要刪除mysql表上取消選中的複選框的值.. – Ken 2012-04-15 03:29:11

+0

@Ken這並不那麼明顯,因爲那時你所需要的只是SQL代碼而不是HTML的東西。無論如何,我包括一個例子給你。 – Ozzy 2012-04-15 09:42:47

+0

謝謝你的建議..我很感激。 – Ken 2012-04-16 09:43:54

相關問題