I have a fairly simple form and I am trying to send the data provided by the user to a table called contact so far so good. The problem lies when it sent the data since it is not being inserted into the table as it should but it is not showing me an error either and that leaves me without options since I do not know where the problem is happening.
// Si no hay errores, registramos al usuario
if(count($errors) === 0){
$statement = $conexion->prepare('INSERT INTO contact (id_contact, name_c, surname_c, email_c, message_c, browser_contact, ip_contact, create_at_contact) VALUES(
null, :name, :surname, :email_c, :text, :browser, :ip, NOW())'
);
$statement->execute(array(
':name' => $name,
':surname' => $surname,
':email_c' => $email_c,
':text' => $text,
':browser' => $browser,
':ip' => $ip
));
$success = "$name, tu mensaje fue enviado con éxito!";
}
Here basically what I am saying is that if there are no errors that I insert the data in the contact table and then send myself a success message and print it on the screen. And indeed it prints me the success message but it doesn't insert the data in the table.
Here you can see that nothing is getting to the database and you can also see the names of the fields and as far as I have asked to check the names of the fields are correctly written with those in the PHP code
It seems you have an error in your SQL statement . Making a comparison with the image of the contact table of the database, I have noticed that among all the columns you have a call
create_at_contact
but in your SQL statement you misspelled the name of this and putcrate_at_contact
.Try placing the name correctly and that will solve your error.
Hi friend, I'm not that experienced, but your code seems to be fine, you could try without the error conditional, and another point would be that if your table has the autoincrementing id, try without passing it null or specifying id_contact, because it should be inserted automatically, greetings friend.
You can change the php.ini variable "display_errors" with
ini_set('display_errors', '1');
to display errors in the browser.Or view the logs in
/var/log/apache2/error.log
(or where appropriate in your operating system). I use Ubuntu and I put this command in the terminal so that it shows me the errors:tail -f /var/log/apache2/error.log | grep php:error
(with the -f option it reads the updates from the file)On Windows 10 I think it would be this file:
'C:\Program Files\Apache Software Foundation\Apache2.2\logs'
And with Xampp it should be something like this:
\xampp\apache\logs\error.log
You can find the location of the file using
phpinfo();
and searchingAPACHE_LOG_DIR
.I don't know what it is
$errors
in your context:The truth is that this does not serve to control the errors that could occur later, preparing and executing the query . Doing both could cause errors (syntax, duplicate key, etc) and you should handle them.
If you want controlled code, you could do something like this. Here we use an array
$msg
where we will put a keysuccess
orerror
depending on what happens. And the content of those keys will be a descriptive message. In the case of errors I have usederrorInfo()
to debug. In production, it is convenient to change it to custom messages, since it is not convenient to show the client internal error messages of the handler or the API.