违反完整性约束:1048列“名称"不能为空 [英] Integrity constraint violation: 1048 Column 'name' cannot be null

查看:285
本文介绍了违反完整性约束:1048列“名称"不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用REST API使用PHPSlim框架注册用户. 当我在高级REST客户端中运行它时,它给我一个错误:

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() .'}}'; 
    }
}

堆栈跟踪:

#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}

如何解决此错误?

推荐答案

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

这篇关于违反完整性约束:1048列“名称"不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆