上传文件时如何获取ID

时间:2018-11-20 19:22:10

标签: php forms pdo

这是我的gallery.php;

<br>
<center><form action="galleryuploadxd.php" method="post" enctype="multipart/form-data">
        Category
        <select>
        <option value="">Select...</option>
        <option id_image="1" value="1">Admin Images</option>
        <option id_image="2" value="2">User Images</option>
        </select>
        <br><br>

        Select image to upload:
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
    </form></center>

因此,我希望将选项标签id_images写入我的数据库。我做了这样的事情。

galleryuploadxd.php;

<?php
session_start();
if(isset($_SESSION['sess_user_id']) && $_SESSION['sess_user_id'] != "") {
} else {
    header('location:index.php');
}
?>
<body style="background-color: lightgray"></body>
<center><img src="../images/x.png"></center>

<?php
include "db.php";

$id = $_GET['id_image'];
$target_file2 = "random-dir/";
$target_dir = "randomdir/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$target_file3 = $target_file2 .$target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "<br>";
        echo "<br>";
        echo "<h1><center>File is an image - " . $check["mime"] . "." ."</center></h1>";
        $uploadOk = 1;
    } else {
        echo "<br>";
        echo "<h1><center>File is not an image.</center></h1>";
        $uploadOk = 0;
    }
}

if (file_exists($target_file)) {
    echo "<br>";
    echo "<h1><center>Sorry, file already exists.</center></h1>";
    echo "<h1><a href = gallery-edit.php>Go back </a></h1>";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "<br>";
    echo "<h1><center>Sorry, your file is too large.</center></h1>";
    echo "<h1><a href = gallery-edit.php>Go back </a></h1>";
    $uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
    echo "<br>";
    echo "<h1><center>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</center></h1>";
    $uploadOk = 0;
}
if ($uploadOk == 0) {
    echo "<br>";
    echo "<h1><center>Sorry, your file was not uploaded.</center></h1>";
    echo "<br>";
    echo "<br>";
    echo "<br>";
    echo "<h1><center><a href = gallery-edit.php>Go back </a></center></h1>";


} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "<h1><center>The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.</center></h1>";
        echo " <meta http-equiv=\"refresh\" content=\"5;url=gallery-edit.php\" />";
        echo "<center><h1>You Will be redicted to user gallery in 5 seconds...</h1></center>";
        echo "<center><h1>If your browser doesn't support redict please<a href=gallery-edit.php> click here </h1></a></center>";
        $sql = "INSERT INTO categories (id,image_link) VALUES (:id,:image_link)";
        $stmt = $db->prepare($sql);

        $stmt->bindValue(':image_link', $target_file3);
        $stmt->bindValue(':id', $id);
        $result = $stmt->execute();
    } else {
        echo "<br>";
        echo "<center>Sorry, there was an error uploading your file.</center>";
        echo " <center><a href = gallery-edit.php>Go back </a></center>";
    }
}
?>

因此,在galleryuploadxd.php中,我说$id = $_GET['id_image'];开头,然后说

$sql = "INSERT INTO categories (id,image_link) VALUES (:id,:image_link)";
            $stmt = $db->prepare($sql);

            $stmt->bindValue(':image_link', $target_file3);
            $stmt->bindValue(':id', $id);
            $result = $stmt->execute();

我收到id不能为null的错误,为什么没有得到id的人可以帮助我吗?

错误;

  

致命错误:未捕获的PDOException:SQLSTATE [23000]:违反完整性约束:1048 C:\ xampp \ htdocs \ xxx \ xxxx-admin \ galleryuploadxd.php:75中的列'id'不能为空:堆栈跟踪:#0 C:\ xampp \ htdocs \ xxx \ xxxx-admin \ galleryuploadxd.php(75):PDOStatement-> execute()#1 {main}放在C:\ xampp \ htdocs \ xxx \ xxxx-admin \ galleryuploadxd.php上第75行

它告诉我我无法获得id。如何获得多数民众赞成在选项的ID?我需要对上传内容进行分类,这就是为什么我要这样做的原因。

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

为什么不设置所选输入的名称

<select name="id_image">
        <option value="">Select...</option>
        <option value="1">Admin Images</option>
        <option value="2">User Images</option>
 </select>

将此用作$id = $_POST['id_image'];

答案 1 :(得分:-1)

在您的表单中,方法是POST,并且您尝试使用

检索记录
SELECT
   t2.pname,
   COUNT(t.subjectid) AS CountOfsubjectid,
   agg.showntime 
FROM
   table2 AS t2 
INNER JOIN 
    (
     select 
        t1.id,
        max(outcometime) as showntime
     from
        table1 t1 
     group by 
        t1.id                     
     ) AS agg
  ON t2.id = agg.id
WHERE
   t2.outcome = 'accepted'    
GROUP BY
   t2.pname,
   agg.showntime;

您应该这样做:

$id = $_GET['id_image'];