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

    ?>

私が抱えている問題は、新しいレコードを作成すると、データベースに 2 つのレコードが作成されることです。1 つの空のレコードには何も含まれておらず、1 つの有効なレコードには追加されたアルバムのすべての詳細が含まれています。

ここに画像の説明を入力

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 に答える