-1

我正在使用以下表单创建相册,它将数据提交给处理脚本,然后处理文件并将数据输入数据库。

这是提交表格:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Create New Album</title>
    </head>

    <body>
    <p>Create New Album</p>
    <form action="createnewalbumsubmit.php" method="post" enctype="multipart/form-data" name="form1" id="form1">

        <input type="hidden" value="<?php echo substr(md5(time() * rand()),0,10); ?>" name="albumid" id="albumid" />
        <input type="hidden" value="<?php echo date("Y-m-d"); ?>" name="datecreated" id="datecreated" />
      <input type="hidden" value="yes" name="isalbum" id="isalbum" />

      <p>
        <label for="albumname">Album Name</label>
        <input type="text" name="albumname" id="albumname" />
      </p>
      <p>
        <label for="albumthumbnail">Album Thumbnail Image</label>
        <input type="file" name="albumthumbnail" id="albumthumbnail" />
      </p>
      <p>
        <input type="submit" name="submit" id="submit" value="Submit" />
      </p>
    </form>
    </body>
    </html>

这是数据处理脚本,它使用 VEROT 上传类处理上传的文件,然后使用 MYSQLI 向数据库添加详细信息:

    <?php include("connect.php"); ?>
    <?php
    // Posted Data
    if(isset($_POST['albumid'])){
        $albumid = $_POST['albumid'];};

        if(isset($_POST['datecreated'])){
        $datecreated = $_POST['datecreated'];};

        if(isset($_POST['isalbum'])){
        $isalbum = $_POST['isalbum'];};

        if(isset($_POST['albumname'])){
        $albumname = $_POST['albumname'];};
        //


        require_once 'uploadclass/class.upload.php';

        $file = new Upload($_FILES['albumthumbnail']);
    if ($file->uploaded) {
      // save uploaded image with a new name,
      // resized to 100px wide
      $albumthumbnail = substr(md5(time() * rand()),0,10);
      $file->file_new_name_body = $albumthumbnail;
      $file->image_resize = true;
      $file->image_convert = 'jpg';
      $file->image_x = 100;
      $file->image_ratio_y = true;
      $file->Process('albums/'.$albumid.'/thumbnail/');
      $filename = $file->file_dst_name;
      if ($file->processed) {
        echo 'image renamed, resized x=100
              and converted to jpg';
        $file->Clean();
      } else {
        echo 'error : ' . $file->error;
      }
    }

    mysqli_query($db,"INSERT INTO albums (`albumid`,`datecreated`,`isalbum`,`albumname`,`albumthumbnail`) VALUES ('$albumid','$datecreated','$isalbum','$albumname','$filename')");

    ?>

我遇到的问题是,当我创建一条新记录时,数据库中正在创建两条记录,一条空白记录,其中没有任何内容,一条有效记录,其中包含添加专辑的所有详细信息。

在此处输入图像描述

4

1 回答 1

4

这是因为您没有检查表单是否正在发布。每次您登陆页面时,它都会运行:

mysqli_query($db, "INSERT INTO albums (`albumid`,`datecreated`,`isalbum`,`albumname`,`albumthumbnail`) VALUES ('$albumid','$datecreated','$isalbum','$albumname','$filename')");

这就是为什么你得到一个空白记录。您需要用以下方式包围您的提交代码if (!empty($_POST)) { }

<?php
include ("connect.php");

if (!empty($_POST)) {
    // Posted Data
    if (isset($_POST['albumid'])) {
        $albumid = $_POST['albumid'];
    };

    if (isset($_POST['datecreated'])) {
        $datecreated = $_POST['datecreated'];
    };

    if (isset($_POST['isalbum'])) {
        $isalbum = $_POST['isalbum'];
    };

    if (isset($_POST['albumname'])) {
        $albumname = $_POST['albumname'];
    };
    //

    require_once 'uploadclass/class.upload.php';

    $file = new Upload($_FILES['albumthumbnail']);
    if ($file -> uploaded) {
        // save uploaded image with a new name,
        // resized to 100px wide
        $albumthumbnail = substr(md5(time() * rand()), 0, 10);
        $file -> file_new_name_body = $albumthumbnail;
        $file -> image_resize = true;
        $file -> image_convert = 'jpg';
        $file -> image_x = 100;
        $file -> image_ratio_y = true;
        $file -> Process('albums/' . $albumid . '/thumbnail/');
        $filename = $file -> file_dst_name;
        if ($file -> processed) {
            echo 'image renamed, resized x=100
              and converted to jpg';
            $file -> Clean();
        } else {
            echo 'error : ' . $file -> error;
        }
    }

    mysqli_query($db, "INSERT INTO albums (`albumid`,`datecreated`,`isalbum`,`albumname`,`albumthumbnail`) VALUES ('$albumid','$datecreated','$isalbum','$albumname','$filename')");
}
?>
于 2013-03-18T15:08:35.993 回答