I've been dealing with this for several hours now and I can't figure it out. I'm working on a personal project, a good idea, and because I don't want someone to steal my idea or someone else to do it, I'm doing it myself without having studied php or mysql. The worst thing is that I love it but I understand half of the things.
Straight to the point; I'm trying to get a simple form to send the data to my database, but something is wrong. The connection is fine, it was the first thing I saw, but there must be something wrong with the code or something wrong with my database , because either the data does not arrive or is not sent. I attach the codes to see if someone sees something, if not I will see a little more detail.
con_db.php
<?php
$mysqli = new mysqli("localhost","fake_user","fake_pass","mandore2_Alfa");
if ($mysqli -> connect_errno) {
echo "Fallo al conectar a MySQL: " . $mysqli -> connect_error;
exit();
}
?>
index2.php
<!DOCTYPE html>
<html>
<head>
<title>Registrar usuario</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="estilo.css">
</head>
<body>
<form method="post">
<h1>¡Materia!</h1>
<input type="text" name="name" placeholder="Nombre completo">
<input type="text" name="message" placeholder="Mensaje">
<input type="submit" name="register">
</form>
<?php
include("registrar.php");
?>
</body>
</html>
register.php
<?php
include("con_db.php");
if (isset($_POST['register'])) {
if (strlen($_POST['name']) >= 1 && strlen($_POST['message']) >= 1) {
$name = trim($_POST['name']);
$message = trim($_POST['message']);
$fecha = date("d/m/y");
$consulta = "INSERT INTO Materia S1(nombre, mensaje, fecha) VALUES ('$name','$message','$fecha')";
$resultado = mysqli_query($conex,$consulta);
if ($resultado) {
?>
<h3 class="ok">¡Enviado!</h3>
<?php
} else {
?>
<h3 class="bad">¡Ups ha ocurrido un error!</h3>
<?php
}
} else {
?>
<h3 class="bad">¡Por favor complete los campos!</h3>
<?php
}
}
?>
The error that I always get when filling in the fields and pressing "send" is the "bad" that I put up here; "Oops, an error has occurred!"
Update:
I just looked at the error_logs, this shows up:
[17-Sep-2020 04:01:11 UTC] PHP Notice: Undefined variable: conex in ...registrar.php on line 11
[17-Sep-2020 04:01:11 UTC] PHP Warning: mysqli_query() expects parameter 1 to be mysqli, null given in ...registrar.php on line 11
Line 11 of that file would be:
$resultado = mysqli_query($conex,$consulta);
Update 2: I already managed to fix both errors by replacing con_db.php with another code:
<?php
$conex = mysqli_connect("localhost","fake_user","fake_pass","mandore2_Alfa");
?>
But to my bad luck a new error appeared that I don't see any sense in:
[17-Sep-2020 04:17:15 UTC] PHP Notice: Undefined index: email in /home/.../registrar.php on line 6
This line is:
if (strlen($_POST['name']) >= 1 && strlen($_POST['message']) >= 1) {
I attach a screenshot of my table.
Update 3:
I fixed what would be the "date" that they told me was wrong, that line looked like this:
$fecha = date("Y-m-d H:i:s");
I don't think the previous error has been solved or I don't know, but in the error_log no error appears when submitting the form, but the form still gives me an error.
I see you're referencing $conex here:
But you don't have that variable declared, try to do things with patience and following a logic (structured or in objects), don't get confused by the practices you see of complex codes, make your code as simple as possible and then you will improve.
It's my advice.
UPDATE
I see that you renamed the variable $mysqli and saved its value in a variable called $conex, after that I don't see any errors. If it gets too complicated for you, try to do the code again following my guidelines.
successes and blessings