php - 从所选复选框的数据库中选择全部

标签 php javascript mysql

下面给出的代码显示主页中的所有产品..每个产品都有一个复选框..我想选择几个产品的复选框,如果我点击提交,下一页不会显示选中的复选框结果

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

最佳答案

尝试将第二个代码的开头更改为:

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 !

关于php - 从所选复选框的数据库中选择全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15229289/

相关文章:

php - 更改表的显示值(MySQL)

javascript - IE11 : Object doesn't support property or method 'padStart' 怎么解决

javascript - 添加超链接时如何保持图像缩放

mysql - 如何在MySQL中检索group by子句中最大日期的字段基值?

python - 使用 Python 快速插入多列到 Sqlite\Mysql

c# - 使用 C# Windows 窗体应用程序将过滤后的数据从 Crystal 报表导出到 pdf

php - InnoDB 有时会更新和插入丢失的数据(PHP 和 MySQL)

php - 使用 soap 和 wsdl 的网络服务无法正常工作 php

php + mysql 两表排序

javascript - 在 d3.js 中有 child 朝向多边的树(类似于家谱)