在数据库中定义数组

时间:2015-07-06 11:28:59

标签: php arrays database

我从数据库下载了一个zip文件但是无法找到我应该放在$file_names = array()中的内容?运行文件时,显示的错误为No ID Selected

我认为这是因为array()内部没有任何内容。有人可以帮我定义array()吗?

<?php
$file_names = array();
// Make sure an id was passed
if (isset($_GET['id'])) {
    // Get the id into string (in case of an array)
    $id_pass = implode(",",$_GET['id']);
    // Make sure the name is in fact a valid
    if ($id_pass <= 0) {
        die ('No ID Selected!');
    } else {
        // codes to connect to the database is here

        // Fetch the file information
        $query = "select * from docu  where id = {$id_pass};";
var_dump($result);
        $result = $dbLink->query($query);

        if ($result) {
            // Make sure the result is valid
            if ($result->num_rows == 1) {
                // Get the row
                $row = mysqli_fetch_assoc($result);

                //zip function

                $zip = new ZipArchive();
                $filename = "export_" . date('Y.m.d H.i.s') . ".zip";

                if ($zip->open($filename, ZIPARCHIVE::CREATE |   ZIPARCHIVE::OVERWRITE) !== true) {
                    echo "Cannot Open for writing";
                }


                $ext = $row['name'] . ".pdf"; // taking file name from DB and adding extension separately
                $zip->addFromString($ext, $row['content']); //adding blob data from DB
                $zip->close();

                header("Content-disposition: inline; filename='.$filename'");
                header('Content-type: application/zip');
                readfile($filename);
                unlink($filename);
            }
        }

        // Free the mysqli resources     
        mysqli_free_result($result);
        mysqli_close($dbLink);
    }
}

var_dump($result)

的结果

object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(2) ["lengths"]=> NULL ["num_rows"]=> int(22) ["type"]=> int(0) }

这是HTML表单。

<html>
<form action="test.php" method="get"/>
<?php
//codes to connect to the database is here
 mysqli_select_db($con, "images");
 $query = "SELECT id, name FROM docu  order by id";
 $result = mysqli_query($con, $query) or die('Error, query failed');

if (mysqli_num_rows($result) == 0) {
    echo "Database is empty <br>";
} else {
    while ((list($id, $name) = mysqli_fetch_array($result, MYSQLI_BOTH))) {
        echo "<input type='checkbox' name='id[]'>";
        echo $name . " " . $id . " " . "<br>"; //id and name removed
    }
}
?>
   <input type="submit" name="submit" value="Submit" />
</html>

1 个答案:

答案 0 :(得分:1)

这样做。在你的while循环中:

echo "<input type='checkbox' name='$id' value='$id'>";

它应该有用。

说明:当您在服务器上发布表单时,复选框的值将由其中包含的value获取。由于您没有在您的价值中添加任何内容,因此您无法获得服务器上的id