我的网站存在问题。
我明白问题是什么,但我不确定如何解决它。
我所拥有的是个人资料网站,我正在为网站添加照片。
现在当您编辑配置文件时,如果您没有为配置文件提供图像,那么$ 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();
}
感谢您提供任何帮助或指导。
答案 0 :(得分: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();
}
答案 1 :(得分: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
}
}