php-crud-api icon indicating copy to clipboard operation
php-crud-api copied to clipboard

Error 500 while doing a post request

Open xavhoff opened this issue 7 years ago • 20 comments

Hello,

I have a problem while executing a POST request. Here is what my request looks like : const dette = {"id_user":233,"created_dt":"2019-04-09","amount":10,"commentaire":"test","paid":0,"modified_dt":"2019-04-09"}

 fetch('https://www.mydomain.com/api/api.php/records/v2_dettes', {
      method: 'POST',
      body: dette
    })
      .then(() => {})
      .catch(error => {
        console.log(error);
      });
  };

Here is my table : https://screenshots.firefox.com/DhddAg9g0i9gOZHL/phpmyadmin.ovh.net

And what the request respond to me : {"code":9999,"message":"Call to a member function execute() on boolean"}

GET,UPDATE and DELETE are working like a charm. But with the POST request, the api return me an internal error 500 but my record is present in the database ! Do you have any ideas why the api is raising me this error ?

Thanks in advance

xavhoff avatar Apr 09 '19 06:04 xavhoff

Hi, tinyint(1) is mapped with boolean for mysql If you change "paid":0 to "paid":false, it might work

thipages avatar Apr 09 '19 07:04 thipages

@thipages Good suggestion!

mevdschee avatar Apr 09 '19 08:04 mevdschee

First, thanks for you response

When using "paid': false, i got : {"code":9999,"message":"SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'paid' at row 1"}

So 1/0 seems to be the right way to pass the value.

xavhoff avatar Apr 09 '19 08:04 xavhoff

It could very well be a bug, but I would need to reproduce it locally before I can fix it. Does it happen on any tinyint(1)?

mevdschee avatar Apr 09 '19 08:04 mevdschee

I only have one in my db structure. If it's a bugm I can't help you on that because of my poor php skills.

xavhoff avatar Apr 09 '19 08:04 xavhoff

I was not able to reproduce the issue yet, although I have created the same table:

CREATE TABLE `v2_dettes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_user` int(11) NOT NULL,
`created_dt` date NOT NULL,
`modified_dt` date NOT NULL,
`amount` decimal(10,0) NOT NULL,
`commentaire` varchar(255) CHARACTER SET utf8 NOT NULL,
`paid` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
KEY `id_user` (`id_user`),
CONSTRAINT `v2_dettes_ibfk_1` FOREIGN KEY (`id_user`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

then doing:

curl --data '{"id_user":2,"created_dt":"2019-04-09","amount":10,"commentaire":"test","paid":0,"modified_dt":"2019-04-09"}' http://localhost:8000/api.php/records/v2_dettes

resulted in:

INSERT INTO `v2_dettes` (`id`, `id_user`, `created_dt`, `modified_dt`, `amount`, `commentaire`, `paid`) VALUES
(2,	2,	'2019-04-09',	'2019-04-09',	10,	'test',	0);

and there was no error!

So there must be something else different in your setup. Can you check (and post) the response headers (and make sure the "debug" config parameter is set to "true")?

mevdschee avatar Apr 12 '19 08:04 mevdschee

Thanks for the follow up ! Indeed, your setup seems the same.

Here is the response header with the debug true :

HTTP/1.1 500 Internal Server Error Set-Cookie: 60gp=R4109521858; path=/; expires=Fri, 12-Apr-2019 09:26:46 GMT Date: Fri, 12 Apr 2019 08:19:28 GMT Server: Apache X-Powered-By: PHP/7.2 Content-Length: 72 X-Exception-Message: Call to a member function execute() on boolean X-Exception-File: /home/odsladqg/www/api/api.php:2015 Content-Type: application/json X-IPLB-Instance: 504

https://github.com/mevdschee/php-crud-api/blob/master/api.php#L2015

I tried through a browser and directly with curl. Same result.

xavhoff avatar Apr 12 '19 08:04 xavhoff

Can you add this to line 2014 of api.php:

var_dump($sql);

and try this one after the "prepare":

var_dump($stmt->errorInfo());

Hopefuly it will help us to find why the prepare statement is failing.

mevdschee avatar Apr 12 '19 08:04 mevdschee

I don't know why, but the response is not complete.

Response raw: string(113) "INSERT INTO "v2_dettes" ("id_user","created_dt","modified_dt"

Response header:

content-length →74 content-type →application/json date →Fri, 12 Apr 2019 09:09:35 GMT server →Apache x-exception-file →/home/odsladqg/www/api/api.php:2016 x-exception-message →Call to a member function errorInfo() on boolean x-iplb-instance →17329 x-powered-by →PHP/7.2

Maybe I can send you the url of my api by mail ?

xavhoff avatar Apr 12 '19 09:04 xavhoff

the response is not complete.

Comment out this line on line 5834:

$this->addHeader('Content-Length', strlen($data));

Now the response should be complete.

Then change the second debug statement to:

var_dump($this->pdo->errorInfo());

Let's see whether or not that helps to see a proper error message.

Maybe I can send you the url of my api by mail ?

Sure, but sending the URL by email is not helping me much.

mevdschee avatar Apr 12 '19 09:04 mevdschee

string(113) "INSERT INTO "v2_dettes" ("id_user","created_dt","modified_dt","amount","commentaire","paid") VALUES (?,?,?,?,?,?)" array(3) { [0]=> string(5) "00000" [1]=> NULL [2]=> NULL } string(23) "SELECT LAST_INSERT_ID()" array(3) { [0]=> string(5) "00000" [1]=> NULL [2]=> NULL } {"code":9999,"message":"Call to a member function execute() on boolean"}

xavhoff avatar Apr 12 '19 09:04 xavhoff

It is very strange, but the prepare call returns a boolean, while it should throw an error, due to this line on line 1809:

\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,

Is that still there?

mevdschee avatar Apr 12 '19 09:04 mevdschee

Yes. I changed nothing excepts credentials and the middleware cors in the api.php

xavhoff avatar Apr 12 '19 09:04 xavhoff

All I can think of is that the user you are using has no write permissions on the database. Could this be the case? Can you try to manually insert some values? Use this syntax:

INSERT INTO `v2_dettes` (`id_user`, `created_dt`, `modified_dt`, `amount`, `commentaire`, `paid`)
VALUES (223,	'2019-04-09',	'2019-04-09',	10,	'test',	0);

Let me know whether or not that succeeds.

mevdschee avatar Apr 12 '19 09:04 mevdschee

 1 row inserted.
Inserted row id: 469 (Query took 0.0004 seconds.)
INSERT INTO `v2_dettes` (`id_user`, `created_dt`, `modified_dt`, `amount`, `commentaire`, `paid`) VALUES (223, '2019-04-09', '2019-04-09', 10, 'test', 0) 

But remember that the POST to the api is working (the row is added to the db) but it's just that the API raise me an error ! My app using the api is working, but I need to ignore the error given by the api.

xavhoff avatar Apr 12 '19 11:04 xavhoff

Well.. you could add an '@' in front of the offending line, that suppresses the error :-)

mevdschee avatar Apr 12 '19 11:04 mevdschee

I'm out of options to reproduce your problem. Can you help me?

mevdschee avatar Apr 12 '19 17:04 mevdschee

I rarely give up on an error, but this one is particularly hard to reproduce.

I have read somewhere that somebody said that prepare may return false in some occasions:

turns out that one of my field names was incorrect

see: https://stackoverflow.com/questions/1219809/mysqli-prepare-statement-returning-false-but-why#1220197

Maybe you should recheck the query for non-visible characters that are copy pasted in?

I have really out of ideas. Can you help me or did you solve it already?

mevdschee avatar Apr 17 '19 07:04 mevdschee

Hello,

Sorry I'm a little bit out of time atm. I will double check for invisible characters as soon as possible. Thanks for you help !

xavhoff avatar Apr 17 '19 08:04 xavhoff

I'm closing this issue for now. Feel free to report new insights.

mevdschee avatar Apr 22 '19 21:04 mevdschee