dongshi2458 2015-10-02 04:54
浏览 137
已采纳

完整性约束违规:1048列“名称”不能为空

I'm making a REST API to register a user using PHP and Slim framework. It's giving me an error when I run it in advanced REST client:

{"error":{"text":SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null}}

The code for the register module is as follows:

function insertUpdate() {
    $request = \Slim\Slim::getInstance()->request();
    $update = json_decode($request->getBody());
    $sql = "INSERT INTO users (name,email,password,phoneNumber,imageUrl,created_at) VALUES (:name, :email, :password, :phoneNumber, :imageUrl, :created_at)";
    try {
        $db = getDB();
        $stmt = $db->prepare($sql);  
        $stmt->bindParam("name", $update->name);
        $stmt->bindParam("email", $update->email);
        $stmt->bindParam("password",$update->password);
        $stmt->bindParam("phoneNumber",$update->phoneNumber);
        $stmt->bindParam("imageUrl",$update->imageUrl);
        $time=time();
        $stmt->bindParam("created_at", $time);

        $stmt->execute();
        echo "Register successfull";
    } catch(PDOException $e) {
        //error_log($e->getMessage(), 3, '/var/tmp/php.log');
        echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    }
}

Stack Trace:

#0 /Applications/XAMPP/xamppfiles/htdocs/api/v1/index.php(71): Slim\Slim::handleErrors(8, 'Trying to get p...', '/Applications/X...', 71, Array)
#1 [internal function]: insertUpdate()
#2 /Applications/XAMPP/xamppfiles/htdocs/api/v1/Slim/Route.php(462): call_user_func_array('insertUpdate', Array)
#3 /Applications/XAMPP/xamppfiles/htdocs/api/v1/Slim/Slim.php(1326): Slim\Route->dispatch()
#4 /Applications/XAMPP/xamppfiles/htdocs/api/v1/Slim/Middleware/Flash.php(85): Slim\Slim->call()
#5 /Applications/XAMPP/xamppfiles/htdocs/api/v1/Slim/Middleware/MethodOverride.php(92): Slim\Middleware\Flash->call()
#6 /Applications/XAMPP/xamppfiles/htdocs/api/v1/Slim/Middleware/PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call()
#7 /Applications/XAMPP/xamppfiles/htdocs/api/v1/Slim/Slim.php(1271): Slim\Middleware\PrettyExceptions->call()
#8 /Applications/XAMPP/xamppfiles/htdocs/api/v1/index.php(14): Slim\Slim->run()
#9 {main}

How to resolve this error?

  • 写回答

1条回答

  • dqusbxh44823 2015-10-02 05:54
    关注
    function insertUpdate() {
            $request = \Slim\Slim::getInstance()->request();
            parse_str($request->getBody(),$update); 
            $sql = "INSERT INTO users (name,email,password,phoneNumber,imageUrl,created_at) VALUES (:name, :email, :password, :phoneNumber, :imageUrl, :created_at)";
            try {
                //Validate your filed before insert in db
                if(!is_array($update) || (!$update)) {
                    throw new Exception('Invalid data received');   
                }
                // put your require filed list here
                if((!$update['name']) || (!$update['email']) || (!$update['password'])){
                    throw new Exception('Missing values for require fields');
                }
                $db = getDB();
                $stmt = $db->prepare($sql);  
                $stmt->bindParam("name", $update['name']);
                $stmt->bindParam("email", $update['email']);
                $stmt->bindParam("password",$update['password']);
                $stmt->bindParam("phoneNumber",$update['phoneNumber']);
                $stmt->bindParam("imageUrl",$update['imageUrl']);
                $time=time();
                $stmt->bindParam("created_at", $time);
    
                $stmt->execute();
                echo "Register successfull";
            } catch(PDOException $e) {
                //error_log($e->getMessage(), 3, '/var/tmp/php.log');
                echo '{"error":{"text":'. $e->getMessage() .'}}'; 
            }catch(Exception $e) {
                //error_log($e->getMessage(), 3, '/var/tmp/php.log');
                echo '{"error":{"text":'. $e->getMessage() .'}}'; 
            }
        }
    

    Updated code as per request recieved

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划