Correctly insert data into table

Asked

Viewed 70 times

0

I’m in serious trouble with the INSERT of PDO, I am unable to add the values correctly to the banco de dados, how to proceed in this situation?

Picture of the problem: inserir a descrição da imagem aqui

I’m waiting for help.

<?php
if (isset($_POST['btn-send-ticket'])) {
    $name = filter_input(INPUT_POST, 'name');
    $subject = filter_input(INPUT_POST, 'subject');
    $categorys = filter_input(INPUT_POST, 'category');
    $prioritys = filter_input(INPUT_POST, 'prioritys');
    $message = filter_input(INPUT_POST, 'message');

    if (empty($subject)) {
        $error[] = 'The <strong>subject</strong> field can not be empty';
    } elseif (empty($message)) {
        $error[] = 'The <strong>message</strong> field can not be empty';
    } else {
        try {

            $sqli = "INSERT INTO member_ticket (name, subject, category, priority, message, date, status) VALUES (:name, :subject, :category, :priority, :message, NOW(), 0)";

            $insert = $db->prepare($sqli);
            $insert->bindParam(":name", $name, PDO::PARAM_STR);
            $insert->bindParam(":subject", $subject, PDO::PARAM_STR);
            $insert->bindParam(":category", $categorys, PDO::PARAM_STR);
            $insert->bindParam(":priority", $prioritys, PDO::PARAM_STR);
            $insert->bindParam(":message", $message, PDO::PARAM_STR);

            $insert->execute();

            $success[] = 'Your support request has been successfully opened, will soon be answered thanks.';
        } catch (PDOException $e) {
            echo $e->getMessage() . '<br/>' . $e->getLine();
            exit;
        }
    }
}
?>
  • Thanks for commenting, I added an update to the topic. I don’t know how to properly explain my problem, but I tried to see: Note that when passing :name, :Subject, etc... to the query, the PDO is not treating the query, is inserting what was to be treated.

  • 1

    Now I understand :D

1 answer

3


Remove quotes from named placedholders as you do not need

Change:

VALUES (':name', ':subject', ':category', ':priority', ':message', NOW(), 0)";

For:

VALUES (:name, :subject, :category, :priority, :message, NOW(), 0)";
  • I’ve made this form, return me a warning: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null&#xA;82

  • @Guilhermegouveiaalves ai is another error elsewhere. What’s in the $name variable when you took the test?

  • @Guilhermegouveiaalves needs to verify the value of $name because it came blank.

  • Look guys, I added the código whole on the main topic to get better understanding to you.

  • @William add more elseif (empty($name)) and to the other missing fields, to make sure that everything is coming filled.

  • I added @Bacco, this time sent, left the field as disabled and used the class hidden of Bootstrap there worked.

  • 2

    @Guilhermegouveiaalves, field marked as disabled is not sent, if you want to leave it unchangeable, use readonly

Show 2 more comments

Browser other questions tagged

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