2013-03-05 101 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

相关问题