data is not inserted to the MySQL database

hi all.

please help me out with my issue. I’ll be happy as hell :slight_smile:

I’ve tried to make a block for comments on my website.

steps to reproduce:

  1. I’ve created a table in MySQL database:

    CREATE TABLE messages (
    id int(3) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    author varchar(30) NOT NULL,
    message text NOT NULL,
    date varchar(25) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

  2. here is the form:

    Имя:

    Комментарий:

  3. sending data to database (sendMessage.php file):

    <?php $author = $_POST["author"]; $message = $_POST["message"]; $date = date; $author = htmlspecialchars($author); $message = htmlspecialchars($message); $mysqli = new mysqli("my hostName", "my userName", "my password", "my databaseName"); if ($mysqli->connect_error) { die("Connection failed: " . $mysqli->connect_error);    } $mysqli->query("INSERT INTO `messages` (`author`, `message`, `date`) VALUES (`$author`, `$message`, `$date`)"); ?>

actual result: nothing is stored to the database

software: php v.7.3

additional information: the thing is, that there are no errors in the Console or Network (DevTools)

I’d start by checking the result from the query() call:

if (!$mysqli->query("INSERT INTO `messages` (`author`, `message`, `date`) VALUES (`$author`, `$message`, `$date`)")) {
&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;"Table creation failed:&nbsp;("&nbsp;.&nbsp;$mysqli-\>errno&nbsp;.&nbsp;")&nbsp;"&nbsp;.&nbsp;$mysqli-\>error;
}

Then I’d try hard-coded VALUES settings. Did author, message or date end up being NULL (which violates the table setup)? [You’ll want to limit the input field lengths so the value doesn’t violate the database constraints as well.]

You probably want the id column to be INT and not int(3).

Not certain about this, but it’s possible your line should read:

$mysqli->query("INSERT INTO messages (author, message, date) VALUES (`$author`, `$message`, `$date`)")

The quotes have me confused but I’m not a PHP expert.

1 Like