简体   繁体   English

PHP-CRUD-API 在列上插入 NULL

[英]PHP-CRUD-API inserting NULL on columns

I'm using ( PHP-CRUD-API ) with a MySQL database.我将( PHP-CRUD-API )与 MySQL 数据库一起使用。

When I sent a simple POST passing in the body a JSON object, VARCHAR(255) columns of the table receive NULL values.当我发送一个简单的 POST 在正文中传递一个 JSON 对象时,表的 VARCHAR(255) 列接收 NULL 值。

This is my CREATE TABLE statement这是我的 CREATE TABLE 语句

CREATE TABLE users (   id integer NOT NULL AUTO_INCREMENT,  name varchar(255) ,  email varchar(255),  PRIMARY KEY (id)  ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

When I send a POST passing in the body当我发送一个传入正文的 POST 时

{"id": 1, "name": "Someone", "email": "something@x.com"}

it returns status 200, the row is inserted in the table, but NAME and EMAIL columns has NULL values.它返回状态 200,该行已插入表中,但 NAME 和 EMAIL 列具有 NULL 值。

When I send a GET everything is correct (I've inserted manually some rows to test this).当我发送 GET 时,一切都是正确的(我手动插入了一些行来测试)。

What am I missing?我错过了什么?

EDITED: If I sent the data as x-www-form-urlencoded on the request, it works.编辑:如果我在请求中将数据作为 x-www-form-urlencoded 发送,它就可以工作。 I prefer to pass a JSON object in the body, but...我更喜欢在正文中传递一个 JSON 对象,但是...

This behavior is the default behavior of your MySQL setup on inserting with missing values.此行为是您的 MySQL 设置在插入缺失值时的默认行为。 If you want empty strings, you should add "NOT NULL" before the "varchar" type definition.如果你想要空字符串,你应该在“varchar”类型定义之前添加“NOT NULL”。

NB: I'm the author of PHP-CRUD-API注意:我是 PHP-CRUD-API 的作者

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

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