mevdschee / php-crud-ui

Single file PHP script that adds a UI to a PHP-CRUD-API project
MIT License
123 stars 55 forks source link

Warning: Invalid argument supplied for foreach() #26

Closed anushaath closed 7 years ago

anushaath commented 7 years ago

Same as my last issue.

[01-Apr-2017 10:09:43 UTC] PHP Notice: Undefined index: columns in /home/public_html/ui1.php on line 217 [01-Apr-2017 10:09:43 UTC] PHP Warning: Invalid argument supplied for foreach() in /home/public_html/ui1.php on line 217 [01-Apr-2017 10:09:43 UTC] PHP Notice: Undefined index: records in /home/public_html/ui1.php on line 225 [01-Apr-2017 10:09:43 UTC] PHP Warning: Invalid argument supplied for foreach() in /home/public_html/ui1.php on line 225

Those lines: foreach ($data[$subject]['columns'] as $i=>$column) { foreach ($data[$subject]['records'] as $record) {

in the listRecords()

mevdschee commented 7 years ago

Thank you for creating an issue. I need a bit more information in order to be able to help you.

I expect that the same solution that you applied in #25 did not work?

Can you reply with the exact api call ui.php is doing towards api.php (and the response)?

I think somehow the api is not responding with data, but I need answers on the above to be sure.

anushaath commented 7 years ago

Yes, the solution did not work. And yes you're right again. The API is not responding with the data. This is the postman result.

capture1 capture2

As you can see, the new data with veh_reg_number = 5 did not get reflected on the database.

Edit:- If there is anything more I've to mention, Please say so.

mevdschee commented 7 years ago

@anushaath I think we need to debug the query. Can you uncomment the following line and do the postman request again? It should show the executed SQL, that you can then try to execute manually.

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

I think the query that you try to execute is not valid (due to a constraint or missing value for instance).

anushaath commented 7 years ago

capture3

Now the query is executed because in the previous query, veh_reg_number = 5 didnot exist.

But the problem persists while inserting data using the ui.php.

Just a little bit of background into this. The car_insurance table has a foreign key 'veh_reg_number'. This is taken from the table 'car' where 'car_id' is the primary key, but 'car_reg_number' is not.

My guess is, the ui.php is sending the 'veh_reg_number' value as '5', which is the 'car_id' rather than '555kkk' which is actually the 'car_reg_number'.

Do you think this is a problem?

mevdschee commented 7 years ago

the code will base what it sends on the FK constraint/relation. Did you set that correctly? Can you share the SQL create statements for the tables and their relations?

anushaath commented 7 years ago

SQL statement for the car table:

`CREATE TABLE IF NOT EXISTS `car` (
`reg_number` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
  `car_id` int(6) NOT NULL AUTO_INCREMENT,
  `make` text COLLATE utf8_unicode_ci NOT NULL,
  `model` varchar(25) CHARACTER SET latin1 NOT NULL,
  `colour` varchar(10) CHARACTER SET latin1 NOT NULL,
  `type_of` int(11) NOT NULL,
  `seating_capacity` varchar(5) COLLATE utf8_unicode_ci DEFAULT NULL,
  `internal_height` decimal(6,2) NOT NULL,
  `permit` text COLLATE utf8_unicode_ci NOT NULL,
  `location` varchar(25) CHARACTER SET latin1 NOT NULL,
  `active` tinyint(1) NOT NULL,
  PRIMARY KEY (`car_id`),
  UNIQUE KEY `reg_number_3` (`reg_number`),
  KEY `reg_number` (`reg_number`),
  KEY `reg_number_2` (`reg_number`),
  KEY `type_of` (`type_of`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ;`

SQL statement for car_insurance:

`CREATE TABLE IF NOT EXISTS `car_insurance` (
 `id` int(5) NOT NULL AUTO_INCREMENT,
  `veh_reg_number` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
  `insurance_no` int(11) NOT NULL,
  `insur_due_date` date NOT NULL,
  `insured_company` text COLLATE utf8_unicode_ci,
  `fitness_due_date` date NOT NULL,
  `tax_due_date` date NOT NULL,
  `chassis_number` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  KEY `veh_reg_number` (`veh_reg_number`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10 ;`

The constraints:

-- Constraints for table `car_insurance`

ALTER TABLE `car_insurance`
  ADD CONSTRAINT `car_insurance_ibfk_1` FOREIGN KEY (`veh_reg_number`) REFERENCES
  `car` (`reg_number`) ON DELETE CASCADE ON UPDATE CASCADE;`
mevdschee commented 7 years ago

I see no problem in the data structure. Can you try to figure out the update/insert SQL statement that is failing? You can record all executed SQL, just uncomment:

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

and then replay log.txt using your SQL editor.

anushaath commented 7 years ago

Yes, the insert statement for this is: INSERT INTO "car_insurance" ("veh_reg_number","insurance_no","insur_due_date","insured_company","fitness_due_date","tax_due_date","chassis_number") VALUES ('4','655665','2017-04-14','AVIVA','2017-05-06','2019-08-04','fdh242')

I tried running it in phpMyAdmin capture4

anushaath commented 7 years ago

If you notice, the value for veh_reg_number that was executed was '4', but should actually be '555kkk'.

That is why this statement is failing.

mevdschee commented 7 years ago

I'm sorry for the late reply, but foreign key to non-primary keys of other tables are not supported.

anushaath commented 7 years ago

Yes, I figured it out and changed the foreign key to a primary key.