0

我正在整理的网站存在问题。

我了解问题所在,但不确定如何解决。

我拥有的是个人资料网站,我正在向该网站添加照片。

现在,当您进入编辑配置文件时,如果您没有为配置文件提供图像,则 $FILE[] 数组为空,因此它使用空白值填充数据库,实际上删除了我的照片。

我想做类似的事情:

if $uploadfile = " " DO NOT WRITE ANYTHING TO THE DB.

你知道,$uploadfile = $_FILES['upload']['tmp_name'];

我的代码如下所示:

if (isset($_POST['action']) and $_POST['action'] == 'upload')
{
  $uploadfile = $_FILES['upload']['tmp_name'];
  $uploadname = $_FILES['upload']['name'];
  $uploadtype = $_FILES['upload']['type'];
  $uploaddata = file_get_contents($uploadfile);
}

和编辑代码看起来像这样

    // EDIT A PLAYER PROFILE

    if (isset($_POST['action']) and $_POST['action'] == 'Edit' )
    {
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
        try
        {
            // $sql = 'SELECT id, name, age, position, height, weight, satscore, gpa FROM player WHERE id = :id';
            $sql = 'SELECT player.id, player.name AS name, age, position, height, weight, previousclubs.id AS previousclubsid, GROUP_CONCAT(distinct previousclubs.name) previousclubs, 
                satscore, gpa, GROUP_CONCAT(distinct link) link, email, filename, mimetype, filedata
                FROM player INNER JOIN playerpreviousclubs
                    ON player.id = playerid
                INNER JOIN previousclubs
                    ON previousclubid = previousclubs.id
                INNER JOIN links
                    ON links.playerid = player.id
                WHERE player.id = :id';
            $s = $pdo->prepare($sql);
            $s->bindValue(':id', $_POST['id']);
            $s->execute();
        }
        catch (PDOException $e)
        {
            $error = 'Error fetching profile details.' . $e->getMessage();
            include 'error.html.php';
            exit();
        }

        $row = $s->fetch();
        $pageTitle = 'Edit Profile';
        $action = 'editform';
        $name = $row['name'];
        $age = $row['age']; 
        $position = $row['position'];
        $height = $row['height'];
        $weight = $row['weight'];
        $satscore = $row['satscore'];
        $gpa = $row['gpa'];
        $previousclubs = $row['previousclubs'];
        $previousclubsid = $row['previousclubsid'];
        $link = $row['link'];
        $email = $row['email'];
        $filename = $row['filename'];
        $mimetype = $row['mimetype'];
        $filedata = $row['filedata'];
        $id = $row['id'];
        $button = 'Update Profile';

        include 'addplayerprofile.html.php';
        exit();
    }

    if (isset($_GET['editform']))
    {
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';

        // UPDATE MAIN PLAYER PROFILE DETAILS
        try
        {
            $sql = "UPDATE player SET
                name = :name,
                age = :age,
                position = :position,
                height = :height,
                weight = :weight,
                satscore = :satscore,
                gpa = :gpa,
                email = :email,
                    filename = :filename,
                    mimetype = :mimetype,
                    filedata = :filedata
                WHERE id = :id";
            $s = $pdo->prepare($sql);
            $s->bindValue(':id', $_POST['id']);
            $s->bindValue(':name', $_POST['name']);
            $s->bindValue(':age', $_POST['age']);
            $s->bindValue(':position', $_POST['position']);
            $s->bindValue(':height', $_POST['height']);
            $s->bindValue(':weight', $_POST['weight']);
            $s->bindValue(':satscore', $_POST['satscore']);
            $s->bindValue(':gpa', $_POST['gpa']);
            $s->bindValue(':email', $_POST['email']);
                    $s->bindValue(':filename',  $uploadname);
                    $s->bindValue(':mimetype',   $uploadtype);
                    $s->bindValue(':filedata',   $uploaddata);
            $s->execute();
        }
        catch (PDOException $e)
        {
            $error = 'Error editing player profile main details.' . $e->getMessage();
            include 'error.html.php';
            exit();
        }

        // UPDATE PREVIOUS CLUBS
        try
        {
            $sql = 'UPDATE previousclubs SET
                name = :previousclubs
                WHERE id = :previousclubsid';
            $s = $pdo->prepare($sql);
            $s->bindValue(':previousclubs', $_POST['previousclubs']);
            $s->bindValue(':previousclubsid', $_POST['previousclubsid']);
            $s->execute();
        }
        catch (PDOException $e)
        {
            $error = 'Error editing player previous clubs.' . $e->getMessage();
            include 'error.html.php';
            exit();
        }   

header('Location: .');
exit();
}

感谢您的任何帮助或指导。

4

2 回答 2

0

不确定,但如果您只想检查文件是否已上传,您可以将第一行替换为:

if (isset($_POST['action']) && $_POST['action'] == 'upload' && isset($_FILES['upload']))

或者,如果您想在数据库更新之外正常执行代码:

if (isset($_POST['action']) && $_POST['action'] == 'upload') {
    // Some code
    if (isset($_FILES['upload'])) {
        // Database Update
    }
}
于 2012-09-01T22:52:51.250 回答
0

只需在此处放一个简单的if声明:

try
    {
        $photo_string = (isset($uploadfile)) ? ",
                filename = :filename,
                mimetype = :mimetype,
                filedata = :filedata" : null;

        $sql = "UPDATE player SET
            name = :name,
            age = :age,
            position = :position,
            height = :height,
            weight = :weight,
            satscore = :satscore,
            gpa = :gpa,
            email = :email
            WHERE id = :id $photo_string";
        $s = $pdo->prepare($sql);
        $s->bindValue(':id', $_POST['id']);
        $s->bindValue(':name', $_POST['name']);
        $s->bindValue(':age', $_POST['age']);
        $s->bindValue(':position', $_POST['position']);
        $s->bindValue(':height', $_POST['height']);
        $s->bindValue(':weight', $_POST['weight']);
        $s->bindValue(':satscore', $_POST['satscore']);
        $s->bindValue(':gpa', $_POST['gpa']);
        $s->bindValue(':email', $_POST['email']);
        if (isset($uploadfile)) {
                $s->bindValue(':filename',  $uploadname);
                $s->bindValue(':mimetype',   $uploadtype);
                $s->bindValue(':filedata',   $uploaddata);
        }
        $s->execute();
    }
于 2012-09-01T22:49:08.823 回答