Php 现有用户的Mysql更新/插入

Php 现有用户的Mysql更新/插入,php,mysql,Php,Mysql,我有一个表单,用于获取两个输入:user\u avatar和user\u backgroundpicture。我想从单个表单中更新(如果是现有用户)或插入(如果是首次注册) 这是我的密码: <?php ini_set("display_errors",1); if(isset($_POST)) { require '../_inc/db.php'; $Destination = '../Backgroundimages'; if(!isset($_FILES['BackgroundImag

我有一个表单,用于获取两个输入:user\u avatar和user\u 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');

}  
 ?>
您可以尝试以下方法:

/** 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.*
    函数
  • 试着重写你的代码,使它不那么混乱

还是不行!只有在同时给出用户头像和用户背景图片时才有效。如果只给出一个输入,那么另一个输入将变为空。您对这两个实例都做了建议的更改,对吗?近
mysql\u query($sql5)
也?
if(mysql\u num\u rows($result)>0){if(!empty($\u FILES['BackgroundImageFile']){//备选方案:($\u FILES['BackgroundImageFile']['name']=='')或($\u FILES[“BackgroundImageFile”[“error”]==4)mysql\u query($sql1)或die(mysql\u error());头('location:../input.php?profile=updated');}}else{mysql_query($sql2)或die(mysql_error());header('location:../input.php?profile=notupdated');}
if(mysql_num_rows($result)>0{if(!empty($_FILES['ImageFile']){//备选方案:($_FILES['BackgroundImageFile']['name'=='')或($)u FILES[“BackgroundImageFile”][“error”]==4)mysql_查询($sql5)或die(mysql_error());头('location:../input.php?profile=updated');}}其他{mysql_查询($sql6)或die(mysql_error());头('location:../input.php?profile=notupdated');}
您尝试过任何替代方案吗?
替代方案:($\u文件['BackgroundImageFile']['name']='')或($\u文件['BackgroundImageFile'][“error”]==4)
?我也不确定是否有许多
头()
存在。按照您现在的代码方式,脚本将发生冲突。