Php 通过一次插入创建Mysqli 2记录

Php 通过一次插入创建Mysqli 2记录,php,file-upload,mysqli,insert-into,Php,File Upload,Mysqli,Insert Into,我正在使用以下表单创建一个相册,它将数据提交给处理脚本,然后处理文件并将数据输入数据库 这是提交表格: <!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

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

这是提交表格:

    <!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>

创建新相册
创建新相册


这是因为您没有检查表单是否已发布。每次登录页面时,它都将运行:

mysqli_query($db, "INSERT INTO albums (`albumid`,`datecreated`,`isalbum`,`albumname`,`albumthumbnail`) VALUES ('$albumid','$datecreated','$isalbum','$albumname','$filename')");
这就是为什么你会得到一张空白记录。您需要在提交代码周围加上
if(!empty($\u 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($\u SERVER['request\u METHOD']=='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')");
}
?>