Php 更新数据时,会自动删除以前存储的数据

Php 更新数据时,会自动删除以前存储的数据,php,Php,我目前正在从事一个工作门户项目,在那里我可以存储用户信息, 在我的项目注册后,用户可以去那个里的仪表板,并更新那个里剩余的表单,如,教育详细信息和公司详细信息。但在这之后,当用户想要更新表单中任何一个字段时,它可以更新该字段,但可以删除我的剩余字段,即教育详细信息字段或公司详细信息字段。发生了什么样的问题 updateprofile.php 使用准备好的语句和动态字段映射,只更新其中有值的字段,下面是代码的外观 <?php session_start(); if (empt

我目前正在从事一个工作门户项目,在那里我可以存储用户信息, 在我的项目注册后,用户可以去那个里的仪表板,并更新那个里剩余的表单,如,教育详细信息和公司详细信息。但在这之后,当用户想要更新表单中任何一个字段时,它可以更新该字段,但可以删除我的剩余字段,即教育详细信息字段或公司详细信息字段。发生了什么样的问题

updateprofile.php


使用准备好的语句和动态字段映射,只更新其中有值的字段,下面是代码的外观

<?php
    session_start();
    if (empty($_SESSION['id_user'])) {
        header("Location: ../index.php");
        exit();
    }
    require_once("../db.php");

    if (isset($_POST)) {
        $uploadOk = true;


        if (is_uploaded_file($_FILES['resume']['tmp_name'])) {
            $folder_dir = "../uploads/resume/";
            $base = basename($_FILES['resume']['name']);
            $resumeFileType = pathinfo($base, PATHINFO_EXTENSION);
            $file = uniqid() . "." . $resumeFileType;
            $filename = $folder_dir . $file;
            if (file_exists($_FILES['resume']['tmp_name'])) {

                if ($resumeFileType == "pdf") {
                    if ($_FILES['resume']['size'] < 500000) {
                        // File size is less than 5MB
                        move_uploaded_file($_FILES["resume"]["tmp_name"], $filename);
                    } else {
                        $_SESSION['uploadError'] = "Wrong Size of file. Max Size Allowed : 5MB";
                        header("Location: edit_profile.php");
                        exit();
                    }
                } else {
                    $_SESSION['uploadError'] = "Wrong Format of file only pdf Allowed.";
                    header("Location: edit_profile.php");
                    exit();
                }


            }

        } else {
            $uploadOk = false;
        }

        //Update User Details Query
        $postf2sqlf = array(
            'firstname'        => 'firstname',
            'lastname'         => 'lastname',
            'gender'           => 'gender',
            'contactno'        => 'contactno',
            'address'          => 'address',
            'city'             => 'city',
            'state'            => 'state',
            'aboutme'          => 'aboutme',
            'qualification'    => 'qualification',
            'stream'           => 'stream',
            'coursetype'       => 'coursetype',
            'university'       => 'university',
            'passingyear'      => 'passingyear',
            'skill'            => 'skill',
            'industry'         => 'industry',
            'functional_area'  => 'function_area',
            'role'             => 'role',
            'is_current_job'   => 'is_current_job',
            'startdate'        => 'startdate',
            'enddate'          => 'enddate',
            'current_compname' => 'current_compname',
            'current_salary'   => 'current_salary',
            'designation'      => 'designation',
            'notice_period'    => 'notice_period',
            'job_desc'         => 'job_desc',
            'experience'       => 'experience',
            'current_location' => 'current_location',
            'prefer_location'  => 'prefer_location'
        );

        $sql = 'UPDATE `user` SET ';
        $skipComma = true;
        $params = array('');
        foreach ($postf2sqlf as $p => $s) {
            if (isset($_POST[$p]) && !empty($_POST[$p])) {
                $sql .= ($skipComma ? '' : ',') . '`' . $s . '` = ?';
                $params[] = &$_POST[$p];
                $params[0] .= 's';
                $skipComma = false;
            }
        }

        if ($uploadOk == true) {
            $sql .= ",resume=?";
            $params = &$file;
            $params[0] .= 's';
        }
        $sql .= " WHERE id_user=?";
        $params[0] .= 's';
        $params[] = &$_SESSION['id_user'];

        $stmt = $db->prepare($sql);
        call_user_func_array(array($stmt, 'bind_param'), $params);
        $res = $stmt->execute();


        if ($stmt->errno == 0) {
            //If data Updated successfully then redirect to dashboard
            header("Location: index.php");
            exit();
        } else {
            echo "Error " . $sql . "<br>" . $conn->error;
        }

        //Close database connection.
        $conn->close();
    } else {
        //redirect them back to dashboard page if they didn't click update button
        header("Location: edit_profile.php");
        exit();
    }

执行该sql代码时,'$sql'的运行时值是多少?是否有空白值?您是否希望这些值不为空?为什么?你经常通过让别人为你写文章来交换账户来完成你的项目吗?不是吗?@Cemal有一些区别。不过,可能正在处理同一个作业。另外,为了不在不需要的地方进行更新,您需要在
$\u POST
中的字段为空的地方动态构建sql。@PatrickQ如果我再搜索一点,我很确定我可以在另一个问题的答案部分找到这些不同的部分
<?php
    session_start();
    if (empty($_SESSION['id_user'])) {
        header("Location: ../index.php");
        exit();
    }
    require_once("../db.php");

    if (isset($_POST)) {
        $uploadOk = true;


        if (is_uploaded_file($_FILES['resume']['tmp_name'])) {
            $folder_dir = "../uploads/resume/";
            $base = basename($_FILES['resume']['name']);
            $resumeFileType = pathinfo($base, PATHINFO_EXTENSION);
            $file = uniqid() . "." . $resumeFileType;
            $filename = $folder_dir . $file;
            if (file_exists($_FILES['resume']['tmp_name'])) {

                if ($resumeFileType == "pdf") {
                    if ($_FILES['resume']['size'] < 500000) {
                        // File size is less than 5MB
                        move_uploaded_file($_FILES["resume"]["tmp_name"], $filename);
                    } else {
                        $_SESSION['uploadError'] = "Wrong Size of file. Max Size Allowed : 5MB";
                        header("Location: edit_profile.php");
                        exit();
                    }
                } else {
                    $_SESSION['uploadError'] = "Wrong Format of file only pdf Allowed.";
                    header("Location: edit_profile.php");
                    exit();
                }


            }

        } else {
            $uploadOk = false;
        }

        //Update User Details Query
        $postf2sqlf = array(
            'firstname'        => 'firstname',
            'lastname'         => 'lastname',
            'gender'           => 'gender',
            'contactno'        => 'contactno',
            'address'          => 'address',
            'city'             => 'city',
            'state'            => 'state',
            'aboutme'          => 'aboutme',
            'qualification'    => 'qualification',
            'stream'           => 'stream',
            'coursetype'       => 'coursetype',
            'university'       => 'university',
            'passingyear'      => 'passingyear',
            'skill'            => 'skill',
            'industry'         => 'industry',
            'functional_area'  => 'function_area',
            'role'             => 'role',
            'is_current_job'   => 'is_current_job',
            'startdate'        => 'startdate',
            'enddate'          => 'enddate',
            'current_compname' => 'current_compname',
            'current_salary'   => 'current_salary',
            'designation'      => 'designation',
            'notice_period'    => 'notice_period',
            'job_desc'         => 'job_desc',
            'experience'       => 'experience',
            'current_location' => 'current_location',
            'prefer_location'  => 'prefer_location'
        );

        $sql = 'UPDATE `user` SET ';
        $skipComma = true;
        $params = array('');
        foreach ($postf2sqlf as $p => $s) {
            if (isset($_POST[$p]) && !empty($_POST[$p])) {
                $sql .= ($skipComma ? '' : ',') . '`' . $s . '` = ?';
                $params[] = &$_POST[$p];
                $params[0] .= 's';
                $skipComma = false;
            }
        }

        if ($uploadOk == true) {
            $sql .= ",resume=?";
            $params = &$file;
            $params[0] .= 's';
        }
        $sql .= " WHERE id_user=?";
        $params[0] .= 's';
        $params[] = &$_SESSION['id_user'];

        $stmt = $db->prepare($sql);
        call_user_func_array(array($stmt, 'bind_param'), $params);
        $res = $stmt->execute();


        if ($stmt->errno == 0) {
            //If data Updated successfully then redirect to dashboard
            header("Location: index.php");
            exit();
        } else {
            echo "Error " . $sql . "<br>" . $conn->error;
        }

        //Close database connection.
        $conn->close();
    } else {
        //redirect them back to dashboard page if they didn't click update button
        header("Location: edit_profile.php");
        exit();
    }