Column id with Primary key and auto increment is returning null MYSQL PHP

Asked

Viewed 1,151 times

-2

Good evening guys, it is the following, I have an application that was working until these days, then today when I went to add a post in the database, my id_post that is Primary Key and Auto_increment is not being added, returns the error that my id_post can not be null

here is the error image in the bank

inserir a descrição da imagem aqui

here is the table in the database

CREATE TABLE `posts` (
 `id_post` int(11) NOT NULL AUTO_INCREMENT,
 `id_pergunta` int(11) NOT NULL,
 `texto` varchar(5000) NOT NULL,
 `id_user` varchar(255) NOT NULL,
 `nome` varchar(255) NOT NULL,
 `data` datetime NOT NULL,
 PRIMARY KEY (`id_post`),
 KEY `fk_per_post` (`id_pergunta`),
 CONSTRAINT `fk_per_post` FOREIGN KEY (`id_pergunta`) REFERENCES `pergunta` 
(`id_per`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8

Here the php function to add to the database

function Postar($post){
          $comando = $this->prepare("INSERT INTO posts 
(id_pergunta,texto,id_user,nome,,data)
                                   VALUES (?,?,?,?,now())");

        try{$comando->execute($post);
            return true;
        }
        catch(Exception $e){
            $this->Mensagem = $e->getMessage();
            return false;
        }

    }

And here the php code that sends to the add function in the database

$texto = $_POST["textoresposta"];
$user = $_SESSION["usuario"];
$usuario = $_SESSION["usuario.nome"];
$pergunta = $_POST["idpergunta"];
$comentarioid = $_POST["id_comentario"];
$textocomentario = $_POST["textocomentario"];

    //Aqui fazemos o registro da opnião sobre a pergunta
$postDAO = new PostDAO();
if ($texto == ""){
    echo "<h3>Você tem que escrever alguma coisa</h3>";
}else{
    $postar = array($pergunta,$texto,$user,$usuario);
    $sucesso = $postDAO->Postar($postar);
    if($sucesso){
        header("Location: feed.php");
        exit();
    }else{
        header("Location: index.php?erro=". $postDAO->Mensagem);
    }

}

I don’t understand why the id_post column is returning null if it is an auto increment, if anyone can help me please would be great.

  • 4

    Place the code instead of images.

  • 2

    Kkk seems even combined thing (were not the fact the answer is wrong), pq one puts "Who watches the watchers" in the question and the RORSHACH who answers...

  • ai @sam na real, I put the pictures pq are various codes, and it would be easier to understand. vlw bolsominon

  • @Bacco found incredible this tbm, scared.

  • 4

    @Brunoclementin won’t even correct the posting? This way it’s not cool for those who test or analyze, can barely read, You have to keep enlarging and dragging image, and who is using by mobile hardly see the right post. If you copy and paste from your editor, it makes it easy for everyone (and it has a code formatting button, the bar button { } or control key K, just select the desired code snippet and trigger the shortcut)

  • @Bacco sorry, I made a big mistake :/

  • @RORSCHACH has no reason to apologize, I just warned you because it would not help the post with incorrect information.

  • 1

    https://pt.meta.stackoverflow.com/a/5485/83882

  • 1

    @RORSCHACH but if you can locate the problem can edit and restore your post later, I imagine you already know this (must have to do with a null in a column that does not accept null, so you can barely see in the first image)

  • @Bacco with an explanation of this logic that I change.

  • Already much better with PHP, if you want to do the same in the SQL part, there is an easy way in Phpmyadmin, which is to enter the SQL tab and run SHOW CREATE TABLE redesocial.posts; then the structure comes in SQL ready to copy and paste (then we can even run in a SQL Fiddle to test, for example).

  • Bruno, have you noticed there’s a ,, left in (id_pergunta,texto,id_user,nome,,data)?

  • I decided here, thank you very much for the information but it was a wrong parameter in the name of the form, I found here vlws and I will do this sql @Bacco

  • @Brunoclementin put the solution you found, maybe it will help if someone goes through the same problem

Show 9 more comments

1 answer

0

The solution is just a wrong parameter in the form, it was like this

<div id="posts">    
    <form action="post_grava.php" method="post" id="publicar">
        <input type="hidden" name="idpergunta" value="<?=$pergunta["id_per"];?>"/>
        <textarea class='autoExpand' rows="1" data-min-rows='1' placeholder="O que você pensa sobre isso?" id="textoresposta" name="textocomentario"
            <?php     if(!isset($_SESSION["usuario"]) || !isset($_SESSION["usuario.nome"])) 
            { ?>
            onclick="$('#login').fadeIn(); $('#publicar').hide();" <?php } ?>></textarea> 

          <?php if(isset($_SESSION["usuario"]) || isset($_SESSION["usuario.nome"])) {?>
            <input type="submit" id="submit" value="Publicar"/>
         <?php } ?>

    </form>         
</div>

in the form name is name="text" and the correct name is name="text". running problem

Browser other questions tagged

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