簡體   English   中英

現有用戶的MySQL更新/插入

[英]Mysql update/insert for existing users

我有一個用於獲取兩個輸入的表單: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的操作只是將沒有給出輸入的條目空白。

我想要的是,如果給出一個輸入,則僅應將該輸入插入數據庫。 另一個應保持原樣。

您可以嘗試以下方法:

/** 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_*函數。
  • 嘗試重寫您的代碼以減少混亂。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM