1

The code given below which display all the products in the home page.. and each product has a checkbox.. I wanted to select the checkbox of few products, and if I click on submit, the next page doesn't view the selected checkbox results

$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>';
    }
}

The Code below is where i used to retrieve the data from database of selected checkbox

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( );
}
4

2 回答 2

0

Try changing the beginning of your second code to this:

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 = ...

But, how it was said before, you should start using PDO!

于 2013-03-05T16:52:50.890 回答
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 ;)

-----------------------已编辑------------------------- ------

您的问题出在这一行:

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

那条线是错误的,做这个改变,看看它是如何工作的:

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

萨卢多斯 ;)

于 2013-03-05T16:56:21.137 回答