-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
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.
Place the code instead of images.
– Sam
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...
– Bacco
ai @sam na real, I put the pictures pq are various codes, and it would be easier to understand. vlw bolsominon
– Bruno Clementin
@Bacco found incredible this tbm, scared.
– Bruno Clementin
@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
@Bacco sorry, I made a big mistake :/
– Renato Junior
@RORSCHACH has no reason to apologize, I just warned you because it would not help the post with incorrect information.
– Bacco
https://pt.meta.stackoverflow.com/a/5485/83882
– Renato Junior
@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
@Bacco with an explanation of this logic that I change.
– Bruno Clementin
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).– Bacco
Bruno, have you noticed there’s a
,,
left in(id_pergunta,texto,id_user,nome,,data)
?– Bacco
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
– Bruno Clementin
@Brunoclementin put the solution you found, maybe it will help if someone goes through the same problem
– Renato Junior