从复选框中获取多个记录

时间:2015-12-22 21:29:28

标签: php mysqli

首先,在此之前有人感到后悔并设置此副本我不知道会不会但是我试图搜索很多但是我找不到我的答案我创建了一个表,其中有一些复选框我是什么的我想要做的是,如果有人选择了3个条目,那么数据将被导出为这3个条目只是这是y查询到目前为止我做了什么

$id = $_POST['select'];
    foreach($id as $key) {
        echo $key . ", ";

    }

$sql = mysqli_query($con, "SELECT * FROM $table WHERE uid IN ('$key')") or die(mysqli_error($con));

    $num_rows = mysqli_num_rows($sql);

我不知道为什么它只给我最后一张唱片?它应该给我3条记录,就像在查询之间发生的那样,它给我们在1和3之间的d条目但是如果我们选择id 1 3 5它应该给这3条权利以便将它导出到excel我做了这个代码一些帮助工作正常,但我的查询问题不成功

if($num_rows >= 1)
    {
        $row = mysqli_fetch_assoc($sql);
        $fp = fopen($filename, "w");
        $seperator = "";
        $comma = "";

        foreach ($row as $name => $value)
            {
                $seperator .= $comma . '' .str_replace('', '""', $name);
                $comma = ",";
            }

        $seperator .= "\n";
        fputs($fp, $seperator);

        mysqli_data_seek($sql, 0);
        while($row = mysqli_fetch_assoc($sql))
            {
                $seperator = "";
                $comma = "";

                foreach ($row as $name => $value) 
                    {
                        $seperator .= $comma . '' .str_replace('', '""', $value);
                        $comma = ",";
                    }

                $seperator .= "\n";
                fputs($fp, $seperator);
            }

        fclose($fp);
        echo "Your file is ready. You can download it from <a href='$filename'>here!</a>";
    }
    else
    {
        echo "There is no record in your Database";
    }

1 个答案:

答案 0 :(得分:0)

$key只会是foreach中的最后一个值。给出以下内容:

$id = $_POST['select'];
$key = "";
    foreach($id as $vals) {
        $key = $key . $vals . ",";
    }
$key = trim($key, ",");

$sql = mysqli_query($con, "SELECT * FROM $table WHERE uid IN ('$key')") or die(mysqli_error($con));