【问题标题】:Mysqli 2 records being created with one insertMysqli 2 记录是用一次插入创建的
【发布时间】:2013-03-18 15:05:08
【问题描述】:

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

这是提交表格:

    <!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')");

    ?>

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

【问题讨论】:

    标签: php file-upload mysqli insert-into


    【解决方案1】:

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

    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')");
    }
    ?>
    

    【讨论】:

    • 感谢您的帮助,现在已经整理好了!
    • 请注意,检查它是否是 POST 请求的推荐方法是这样检查: if($_SERVER['REQUEST_METHOD'] == 'POST') ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多