简体   繁体   English

从PHP中不为空的输入将数据插入MySQL数据库

[英]Insert data into MySQL database from inputs that are not empty with PHP

I have page where user can update or change his profile information for example his location or his profile picture. 我有一个页面,用户可以在其中更新或更改其个人资料信息,例如他的位置或个人资料图片。 By default every user get placeholder data or information on his profile page and those fields in mysql database are empty or row with that user id doesn't exists. 默认情况下,每个用户都在其个人资料页面上获取占位符数据或信息,并且mysql数据库中的这些字段为空或不存在具有该用户ID的行。 So first i am checking if user previously entered his profile information into database and if data exist i will just update it with input fields that are not empty and this part works. 因此,首先我要检查用户以前是否将其个人资料信息输入到数据库中,以及是否存在数据,我将使用不为空的输入字段对其进行更新,并且这部分工作正常。 But if data doesn't exists in database or row with this user id doesn't exist then i want to insert data but again only with inputs that are not empty . 但是,如果数据库中不存在数据或具有该用户ID的行不存在,那么我想插入数据,但又只能插入不为空的输入

But this is error i am getting 但这是我得到的错误

Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;... 消息为“ SQLSTATE [42000]”的未捕获的异常“ PDOException”:语法错误或访问冲突:1064您的SQL语法有错误; ...

This is my code ( $userID is session $userID = $_SESSION['ID']; ) 这是我的代码( $userID是会话$userID = $_SESSION['ID'];

if(isset($_POST['updateInfo'])) {
$userDesc = filter_var($_POST['description_update'], FILTER_SANITIZE_STRING);
$userLocation = filter_var($_POST['location_update'], FILTER_SANITIZE_STRING);
$userBirthDate = filter_var($_POST['birthDate_update'], FILTER_SANITIZE_STRING);
$user_picture_tmp = $_FILES['user_picture']['tmp_name'];
$user_picture_name = $_FILES['user_picture']['name'];


$checkInfo = $con->prepare("SELECT * FROM user_info WHERE userid=:userID");
$checkInfo->execute(array(':userID' => $userID));
$data = $checkInfo->fetch(PDO::FETCH_ASSOC);    
$count_rows = $checkInfo->rowCount();

//Here i am checking if data exists in mysql and UPDATING it    
if($count_rows > 0) {   

    if(!empty($userDesc)) {
        $query = $con->prepare("UPDATE user_info SET description=:description WHERE userid=:userID");
        $query->execute(array(':userID' => $userID, ':description'=> $userDesc));
    }   
    if(!empty($userLocation)) {
        $query = $con->prepare("UPDATE user_info SET location=:location WHERE userid=:userID");
        $query->execute(array(':userID' => $userID, ':location'=> $userLocation));
    }
    if(!empty($userBirthDate)) {
        $query = $con->prepare("UPDATE user_info SET birthDate=:birthDate WHERE userid=:userID");
        $query->execute(array(':userID' => $userID, ':birthDate'=> $userBirthDate));
    }
    if(!empty($user_picture_name)) {
        $query = $con->prepare("UPDATE user_info SET profile_pic=:profile_pic WHERE userid=:userID");
        move_uploaded_file($user_picture_tmp, 'user_pic/'.$user_picture_name);
        $query->execute(array(':userID' => $userID, ':profile_pic'=> $user_picture_name));

    }   
    //But if data doesn't exists i want to INSERT IT and here is error
}   else {

    if($userDesc AND $userLocation AND $userBirthDate AND $user_picture_name != '') {

        $query = $con->prepare("INSERT INTO user_info(userid) VALUES(:userID)");
        $query->execute(array(':userID' => $userID));

        if(!empty($userDesc)) {
            $query = $con->prepare("INSERT INTO user_info(description) VALUES(?) WHERE userid=?");
            $query->execute(array($userDesc, $userID));
        }   
        if(!empty($userLocation)) {
            $query = $con->prepare("INSERT INTO user_info(location) VALUES(?) WHERE userid=?");
            $query->execute(array($userLocation, $userID));
        }
        if(!empty($userBirthDate)) {
            $query = $con->prepare("INSERT INTO user_info(birthDate) VALUES(?) WHERE userid=?");
            $query->execute(array($userBirthDate, $userID));
        }   
        if(!empty($user_picture_name)) {
            $query = $con->prepare("INSERT INTO user_info(profile_pic) VALUES(?) WHERE userid=?");
            move_uploaded_file($user_picture_tmp, 'user_pic/'.$user_picture_name);
            $query->execute(array($user_picture_name, $userID));

        }
    } else {echo 'All fields are empty';} 

}                       
}

So my question is why am i getting this error and also am i doing this the wrong way or is there some other better approach to do this. 所以我的问题是,为什么我会收到此错误,我又以错误的方式进行此操作,还是有其他更好的方法来执行此操作? This is just for learning purposes. 这只是出于学习目的。

You're doing a lot of inserts there! 你在那里做很多插入! You should only do one insert! 您只能插入一个!

Try replacing your else-statement with the following: 尝试将您的else语句替换为以下内容:

else {
    if($userDesc AND $userLocation AND $userBirthDate AND $user_picture_name != '') {

        if($userDesc = ''){$userDesc = 'Placeholder description';}
        if($userLocation = ''){$userLocation = 'Placeholder location';}
        if($userBirthDate = ''){$userBirthDate = 'Placeholder birthdate';}
        if($user_picture_name = ''){$user_picture_name = 'Placeholder picture_name';}

        if ($stmt = $con->prepare("INSERT INTO user_info(description, location, birthDate, profile_pic ) VALUES(?,?,?,?)")) {
            $stmt->bind_param("ssss", $userDesc, $userLocation,$userBirthDate, $user_picture_name );
            $stmt->execute();
        }
    } else {echo 'All fields are empty';} 
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM