我有一个用于获取两个输入的表单:user_avatar和user_backgroundpicture。我想从单个表单更新(如果是现有用户)或插入(如果是第一次注册)。
这是我的代码:
<?php
ini_set("display_errors",1);
if(isset($_POST))
{
require '../_inc/db.php';
$Destination = '../Backgroundimages';
if(!isset($_FILES['BackgroundImageFile']) || !is_uploaded_file($_FILES['BackgroundImageFile']['tmp_name']))
{
//die('Something went wrong with Upload!');
$BackgroundNewImageName= 'background4.jpg';
move_uploaded_file($_FILES['BackgroundImageFile']['tmp_name'], "$Destination/$BackgroundNewImageName");
}
else{
$RandomNum = rand(0, 9999999999);
$ImageName = str_replace(' ','-',strtolower($_FILES['BackgroundImageFile']['name']));
$ImageType = $_FILES['BackgroundImageFile']['type']; //"image/png", image/jpeg etc.
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
//Create new image name (with random number added).
$BackgroundNewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
move_uploaded_file($_FILES['BackgroundImageFile']['tmp_name'], "$Destination/$BackgroundNewImageName");
}
require 'authenticationforupload.php';
$sql1="UPDATE user SET user_backgroundpicture='$BackgroundNewImageName' WHERE user_username = '$user_username'";
$sql2="INSERT INTO user (user_backgroundpicture) VALUES ('$BackgroundNewImageName') WHERE user_username = '$user_username'";
$result = mysql_query("SELECT * FROM user WHERE user_username = '$user_username'");
if( mysql_num_rows($result) > 0) {
mysql_query($sql1)or die(mysql_error());
header('location:../input.php?profile=updated');
}
else{
mysql_query($sql2)or die(mysql_error());
header('location:../input.php?profile=notupdated');
}
$Destination = '../uploads';
if(!isset($_FILES['ImageFile']) || !is_uploaded_file($_FILES['ImageFile']['tmp_name']))
{
//die('Something went wrong with Upload!');
$NewImageName= 'default.png';
move_uploaded_file($_FILES['ImageFile']['tmp_name'], "$Destination/$NewImageName");
}
else{
$RandomNum = rand(0, 9999999999);
$ImageName = str_replace(' ','-',strtolower($_FILES['ImageFile']['name']));
$ImageType = $_FILES['ImageFile']['type']; //"image/png", image/jpeg etc.
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
//Create new image name (with random number added).
$NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
move_uploaded_file($_FILES['ImageFile']['tmp_name'], "$Destination/$NewImageName");
}
$sql5="UPDATE user SET user_avatar='$NewImageName' WHERE user_username = '$user_username'";
$sql6="INSERT INTO user (user_avatar) VALUES ('$NewImageName') WHERE user_username = '$user_username'";
$result = mysql_query("SELECT * FROM user WHERE user_username = '$user_username'");
if( mysql_num_rows($result) > 0) {
mysql_query($sql5)or die(mysql_error());
header('location:../input.php?profile=updated');
}
else{
mysql_query($sql6)or die(mysql_error());
header('location:../input.php?profile=notupdated');
}
?>
当上传user_avatar和user_backgroundpicture时,表单正常工作。如果上传了任何一个,而另一个保持不变,则sql的操作只会删除未给出输入的条目。
我想要的是,如果给出一个输入,则只应将该输入插入数据库。另一个应保持不变。
答案 0 :(得分:0)
你可以试试这个:
/** Updates the database only if the file field isn't empty. **/
if( mysql_num_rows($result) > 0) {
if(!empty($_FILES['BackgroundImageFile'])){ // ALTERNATIVES: ($_FILES['BackgroundImageFile']['name']=='') or ($_FILES["BackgroundImageFile"]["error"] == 4)
mysql_query($sql1)or die(mysql_error());
header('location:../input.php?profile=updated');
}
} else {
<强>提示:强>
mysql_*
个功能。