Error entering data into database

Asked

Viewed 237 times

4

I’m following the lessons of php, and in the video the guy uses the command mysql, but I know he’s old and insecure and I used the mysqli, however the code does not present error, but does not save data in my table, follows the form page and the INSERT :

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Comentário</title>
</head>


<body>
    <form method="post" action="#">
        <table>  
        <tr>
             <td><h1>Comente</h1></td>
        </tr>
        <tr>
            <td>Nome</td>
            <td><input type="text" name="txtNome"/></td>
        </tr>
        <tr>
            <td>Email</td>
            <td><input type="email" name="txtEmail"/></td>
        </tr>
        <tr>
            <td>Mensagem</td>
            <td><textarea name="txtMsg"></textarea></td>
        </tr>
        <tr>
            <td>Data</td>
            <td><?php echo date("Y-m-d"); ?></td>
        </tr>
        <tr>
            <td><input type="submit" value="Comentar"/></td>
            <td><input type="reset" value="Cancelar"/></td>
        </tr>
        </table>
    </form>
    <?php
    if(isset($_POST['txtNome']))
        $nome = $_POST['txtNome'];
    else
        $nome = '';
    if(isset($_POST['txtEmail']))
        $email = $_POST['txtEmail'];
    else
        $email = '';
    if(isset($_POST['txtMsg']))
        $msg = $_POST['txtMsg'];
    else
        $msg = '';
    $data = date("Y-m-d");
    $conn = mysqli_connect('localhost', 'root', '', 'aula', '3306');
    if (!$conn) {
        die('Could not connect to MySQL: ' . mysqli_connect_error());
    }
    mysqli_query($conn, 'SET NAMES \'utf8\'');
    $tableQuery = "INSERT INTO `comentario`(`id`, `nome`, `email`, `msg`, `data`) VALUES ('','$nome','$email','$msg','$data')";
    mysqli_query($conn, $tableQuery);
    mysqli_close($conn);
    ?>

</body>


</html>

And the comic book script:

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
--
-- Database: `aula`
--

-- --------------------------------------------------------

--
-- Estrutura da tabela `comentario`
--

CREATE TABLE IF NOT EXISTS `comentario` (
  `id` varchar(10) NOT NULL,
  `nome` varchar(80) DEFAULT 'nome',
  `email` varchar(80) DEFAULT '[email protected]',
  `msg` varchar(1000) DEFAULT 'mensagem',
  `data` date DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

What would be my mistake?

  • Put musqli_error($connection)

  • 1

    Just showed up Duplicata du champ '' pour la clef 'PRIMARY'

  • 1

    The error says the entered code already exists, that’s it. The column id should int, not null and autoincrement.

  • 1

    I recreated the table and it worked, only the id that does not increment, forgot the autoincrement

2 answers

4


The error says that the code entered already exists, it happens because the column id is a scan and its uniqueness in this case depends on the system/application. To leave this responsibility with the database, change this column to int, not null and auto increment

`id` int NOT NULL AUTO_INCREMENT,

2

The error was in the table script, now it looks like this:

--
-- Database: `aula`
--

-- --------------------------------------------------------

--
-- Estrutura da tabela `comentario`
--

CREATE TABLE IF NOT EXISTS `comentario` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nome` varchar(80) DEFAULT NULL,
  `email` varchar(100) DEFAULT NULL,
  `msg` varchar(1000) DEFAULT NULL,
  `data` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

Browser other questions tagged

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