Skip to content
Advertisement

MySQL database is not receiving any data in PHP

I created two classes: a class called index.php for a user to input data such as: name, email, phone number and address. And the other class called model.php, which must send the information that the user typed into the MySQL database. However, when a user enters the information in the graphical interface, and then clicks the submit button, the localhost MySQL database is not receiving the data. The name of the database is called “crud”, and the name of the database table inside the crud is called: “gravacoes”.

Please, can anyone help me?

index.php code:

JavaScript

Code of model.php:

JavaScript

Advertisement

Answer

You are doing approximately everything right, one thing is not right is that your trying to catch Exception from database connection, and mysqli object do not throw any exceptions by default, so try first to enable error reporting:

JavaScript

SQL injection warning:

You should be using prepared statement to insert data to your database to prevent SQL injection like:

JavaScript

That’s could be enough, but I prefer using PDO for this kind of logic, and also you can separate validation from model, so I made you another implementation, not the best but just to put you on the road:

First your model: it’s the same but would be better if you separate db connection from your model:

model.php

JavaScript

then I added a file that handle your form logic

functions.php

JavaScript

Then in your HTML

index.php

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement