如何在没有更新映像的情况下更新mysql中的数据

时间:2013-07-07 17:01:03

标签: php mysql image edit

我想在mysql中更新我的数据。 但是,如果我想要更新(例如名字),photo_profile将丢失。

 <?php
    include 'function_page_user.php';

    if(($_FILES['photo_profile']) and ($_POST['firstname']) and ($_POST['lastname']) and ($_POST['password']))
    {
        session_start();

        include 'connect.php';
        $foldername="assets/img/user/";
        $firstname = mysql_real_escape_string($_POST["firstname"]);
        $lastname = mysql_real_escape_string($_POST["lastname"]);
        $pwd = mysql_real_escape_string($_POST["password"]);

        if((!empty($firstname) and !empty($lastname) and !empty($pwd)) and($_FILES['photo_profile']))
        {
            $image = $foldername . basename ($_FILES['photo_profile'] ['name']);
            mysql_query ("update user set firstname = '".$firstname."' , lastname = '".$lastname."' , password = '".$pwd."' , photo_profile='".$image."' where id_user ='".$_SESSION['id']."'");
            move_uploaded_file($_FILES['photo_profile']['tmp_name'], $image);
            echo "<script>alert ('File Succes To edit');</script>";
            $page="formubahuser.php";

            echo redirectPage($page);   
        }
        else echo "variabel empty";
    }
    else 
        echo ("your data is not complete<a href=formubahuser.php>Fill it again</a>");
?>

1 个答案:

答案 0 :(得分:0)

您的代码中存在许多重大问题。在继续之前,您需要阅读以下主题:

1)不推荐使用php mysql_函数。这意味着在将来的php版本中将删除这些功能。您应该使用pdomysqli代替

2)当您在数据库中存储密码时,它们应始终始终 encrypted

关于您的问题,我想您正在询问如何在不取消设置照片网址的情况下更改元数据(例如名字)。尝试这样的事情:

$updatequery = "UPDATE user SET firstname = '".$firstname."' , lastname = '".$lastname."' , password = '".$pwd."'";
if( $_FILES['photo_profile'])
{ 
   $image = $foldername . basename ($_FILES['photo_profile'] ['name']);
   $updatequery .= ", photo_profile='".$image."'"; 
}
$updatequery .= " where id_user ='".$_SESSION['id']."'";