2013-03-05 110 views
1

下面給出的代碼顯示主頁中的所有產品..每個產品都有一個複選框..我想選擇少數產品的複選框,如果我點擊提交,接下來的頁面沒有查看所選的複選框會導致從選中的複選框的數據庫中選擇全部

$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 15"); 
$productCount = mysql_num_rows($sql); // count the output amount 
if ($productCount > 0) { 
    $colindex = 1; 
    $totcols = 3; 
    $rowclosed = false; 
    //$totrows=ceil($count/$totcols); 
    $dynamicList .= "<dl id='Searchresult'> <form action='selected.php' method='POST'> <table width=\"50%\" border=\"0\" cellspacing=\"0\" align=\"center\" cellpadding=\"3\">"; 
    while ($row = mysql_fetch_array($sql)) { 

     $id = $row["id"]; 
     $product_name = $row["product_name"]; 
     $price = $row["price"]; 
     $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); 
     if ($colindex == 1) { 
      $dynamicList .= "<tr>"; 
      $rowclosed = false; 
     } 

     $dynamicList .= '<td width="142" valign="top" align="center"> 
        <div id="products"> 
         <div class="product"> 
          <a href="product.php?id=' . $id . '"> <img style=" 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="100" height="100" border="0" /></a><div class="pr-info"><h4>' . $product_name . '</h4> 
          <input type="checkbox" name="check[]" value="<?php .$id. ?>"/> 
         </div> 
        </div></td>'; 
    } 
} 

下面的代碼是我用來從選定的複選框的數據庫中的數據

foreach ($_POST['check'] as $k => $check) { 
    $where[ ] = $k . " = '" . mysql_real_escape_string($check) . "'"; 
} 
include "storescripts/connect_to_mysql.php"; 
$sql = mysql_query("Select * from products where " . implode(' AND ', $where)); // Connect to the MySQL database 
$productCount = mysql_num_rows($sql); // count the output amount 
if ($productCount > 0) { 

    while ($row = mysql_fetch_array($sql)) { 

     $id = $row["id"]; 
     $product_name = $row["product_name"]; 
     $price = $row["price"]; 
     $details = $row["details"]; 
     $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); 
     $dynamicList .= '<td width="142" valign="top" align="center"> 
          <div id="products"> 
           <div class="product"> 
            <a href="printer.php?id=' . $id . '"> 
             <img style=" 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="200" height="200" border="0" /> 
            </a> 
           <div class="pr-info"><h4>' . $product_name . '</h4><p>' . $details . ' </p> 

            <span class="pr-price"><span>Rs.</span><sup>' . $price . '</sup></span> 
           </div> 
           </div></td>'; 
    } 
} else { 
    echo "That item does not exist."; 
    exit(); 
} 
+1

首先回顯這些查詢並嘗試直接對數據庫運行它們。其次,mysql_ *被棄用,切換到mysqli_ *或PDO。 – mkaatman 2013-03-05 16:42:24

+0

你確定它們被正確存儲嗎? – 2013-03-05 16:43:12

+0

查詢正在工作,當我選擇複選框並提交,,,我收到我沒有選擇的所有數據 – 2013-03-05 16:46:51

回答

0

試着改變你的第二個代碼開頭對此:

include "storescripts/connect_to_mysql.php"; 
$sql = mysql_query('SELECT * FROM products WHERE id IN ('.implode(', ', array_values($_POST['check'])).')'); 
// rest of your code, beginning at $productCount = ... 

但是,如何說之前,你應該開始使用PDO

+0

這是正確的部分,我已經包含在第一個查詢 」/> – 2013-03-05 17:07:17

+0

不,它不起作用,,,,,如果我提交按鈕,它會顯示else部分,其中說「那個項目不存在」。 – 2013-03-05 17:07:44

+0

這是我的第一個腳本coorect: 」/> – 2013-03-05 17:17:31

0

我覺得你最終查詢和代碼應該是這樣......請改變你的代碼,這... ...和後的結果:

foreach($_POST['check'] as $k){ 
$where[]= $k;} 
var_dump($where); //it show you something, show me the var_dump result ??? 
include "storescripts/connect_to_mysql.php"; 
$query = "Select * from products where id IN(".implode(",",$where).")"; 
echo $query; //this line too....show me the result 
$sql = mysql_query($query);  
// Connect to the MySQL database 
$productCount = mysql_num_rows($sql); 
//the rest of your code ;) 

----------- ------------ EDITED --------------------------------

Your問題是,在這條線:

<input type="checkbox" name="check[]" value="<?php .$id. ?>"/> 

這條線是錯誤的,使這一變化,並看看它是如何工作的:

<input type="checkbox" name="check[]" value="<?php echo $id;?>"/> 

Saludos;)

+0

不,它不起作用,,,,,如果我提交按鈕,它顯示其他部分,,其中說」該項目不存在「。 和錯誤說mysql_num_rows()期望參數1是資源 – 2013-03-05 17:04:46

+0

我添加一行.....你可以顯示var_dump的結果? – Hackerman 2013-03-05 17:07:07

+0

我沒有收到只有錯誤:mysql_num_rows()期望參數1是資源.. 並得到「該項目不存在」 – 2013-03-05 17:16:03