post type variable does not load

Asked

Viewed 33 times

0

Very good evening I’m developing a chat (for study) and I made a form to send the messages as I always do but the php file does not load on the local server of the http 500 error that usually occurs when there is an error in the file but I have already tested in cmd and found nothing

if anyone can help I appreciate

index php.

<form method="post" action="php/insert.php">
                <div class="input-group mb-3">
                            <input type="text" name="mensagem" class="form-control" placeholder="Sua mensagem"  aria-label="Recipient's username" aria-describedby="basic-addon2">
                          <div class="input-group-append">
                            <button class="btn btn-secondary" type="submit">Enviar</button>
                        </div>
                    </div>
                </form>

Insert.php

<?php 
    session_start();
     include("connect.php");     

        $id = isset($_SESSION['id']) ? $_SESSION['id'] : '';
        $nome = isset($_SESSION['name']) ? $_SESSION['name'] : '';      


        if(!empty($id) && !empty($nome) && !empty($_POST["mensagem"])){     

        $sql = $pdo->prepare("INSERT INTO conversas (mensagem, id, nome) VALUES (':mensagem', ':id', ':nome')");
        $sql->bindValue(":mensagem", $_POST["mensagem"]);
        $sql->bindValue(":id",$id);
        $sql->bindValue(":nome", $nome);
        $sql->execute();

        header('location: ../index.php');
        }else {
            $_SESSION['mensagem-not'] = 'É preciso escrever a mensagem';
            header('location: ../index.php');
        }
?>

connect.php

<?php   
    $usr = "root";
    $pwd = "j21042005J$";  


    try {
        $pdo = new PDO('mysql:host=localhost;dbname=chatsimples', $usr, $pwd);
          $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      } catch(PDOException $e) {
          echo 'ERROR: ' . $e->getMessage();
      }
?>

1 answer

0


I believe the error may be because you are not setting the $Pdo variable properly, because you are setting the $Pdo variable inside Try-catch connect.php, but when you try to use it in Insert.php it was not set in the global scope, then the object in

$sql = $pdo->prepare("INSERT INTO conversas (mensagem, id, nome) VALUES (':mensagem', ':id', ':nome')");

is void.

Try adding $Pdo = null; at the beginning of the connect.php file along with the user and password, then it will start in Try-catch and can be used in $sql.

It is also worth you set the

display_errors = On

In PHP.ini to check these errors in more detail.

  • Opa thank you very much I tested here and it worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.