0

Estoy haciendo un chat que consta de 3 partes:

1.- Ingresar el nombre:

    <?php
    include "db.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>proyecto git chat</title>
    <link rel="stylesheet" href="estilos.css">
    <link href="https://fonts.googleapis.com/css?family=Mukta+Vaani" rel="stylesheet">

</head>
<body>
    <div id="contenedor">
        <form method="GET" action="chat.php?$nombre=<?php echo $_POST['nombre']?>&$mensaje=<?php echo $_POST['mensaje']?>">
        <input type='text' name='nombre' placeholder='Ingresa tu nombre'>
        <input type="submit" value="Entrar">
    </div>
</body>
</html>

2.- Aqui se ve el chat y se escriben los mensajes con el nombre que se dio en la anterior parte:

<?php
    include "db.php"
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>chat</title>
    <link rel="stylesheet" href="estilos.css">
    <link href="https://fonts.googleapis.com/css?family=Mukta+Vaani" rel="stylesheet">
</head>
<body>
    <div id="contenedor2">
        <div id="caja-chat">
            <div id="chat">
            <?php
                $consulta = "SELECT * FROM chat ORDER BY id DESC";
                $ejecutar = $conexion->query($consulta);
                while($fila = $ejecutar->fetch_array()):
            ?>
                <div id="datos-chat">
                    <span style="color: #1c62c4"><?php echo $fila['nombre']; ?>:</span>
                    <span style='color: #848484'><?php echo $fila['mensaje']; ?></span>
                    <span style='float: right'><?php echo formatearFecha($fila['fecha']); ?></span>
            <?php endwhile; ?>
                </div>
            </div>
        </div>
        <form action="enviar.php?$mensaje=<?php echo $_POST['mensaje']?>&$nombre=<?php echo $_GET['nombre']?>" method="get">
            <textarea name='mensaje' placeholder="introduce tu mensaje"></textarea>

            <input type="submit" value="Enviar">
        </form>
    </div>
</body>
</html>

3.- Envia el mensaje y el nombre a la base de datos

<?php
    include "db.php";

    $nombre = $_GET["nombre"];
    $mensaje = $_GET["mensaje"];

    $insertar = "INSERT INTO chat (nombre,mensaje) VALUES ('$nombre','$mensaje')";
    $resultado = mysqli_query($conexion, $insertar);

    if ($resultado) {
        echo "";
    } else {
        echo "Error: " . $insertar;
    }

    mysqli_close($conexion);

?>

pero me sale esto en la tercera parte:

Error: INSERT INTO chat (nombre,mensaje) VALUES ('bennjamon','hola')

Y no inserta los datos en la db. la db se llama appchat aqui me conecto a ella y defino unna funcion:

<?php
$servidor = "localhost";
$usuario = "root";
$password = "";
$base_datos = "appchat";

$conexion = new mysqli($servidor, $usuario, $password, $base_datos);


function formatearfecha($fecha){
    return date('g:i a', strtotime($fecha));
}
?>

si alguien sabe que pasa me ayudaria mucho ya quel llevo varias horas intentando esto pero ningun intento funciona

3
  • 1
    ese es todo el mensaje de error ?
    – DarkFrost
    el 16 may. 2020 a las 0:00
  • Suena a nombres de campo incorrectos, ¿podrías aportar la estructura de tu base de datos? (phpMyAdmin, Export)
    – David JP
    el 16 may. 2020 a las 7:16
  • Ya lo arregle . el 17 may. 2020 a las 21:18

0

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

Examina otras preguntas con la etiqueta o formula tu propia pregunta.