-1
How do I select and return more than 1 record does the UPDATE and return 0 records does the INSERT ?
Look how I did it here only it’s not working:
if(isset($_POST['submit'])){
$query = $conexao->prepare("SELECT id_mark, id_user FROM tb_comment WHERE id_mark=:post_id AND id_user=:idLogged");
$query->bindParam(':post_id', $post_id, PDO::PARAM_INT);
$query->bindParam(':idLogged', $idLogged, PDO::PARAM_INT);
$query->execute();
if($result->rowCount() >= 1){
echo '<div class="alert alert-danger">
<strong>Erro!</strong> Não foi possível cadastrar sua avaliação.
</div>';
}
if($result->rowCount() == 0){
$comment = trim(strip_tags($_POST['comment']));
$insert = "INSERT into tb_comment (id_mark, id_user, comment, up_c, down_c, rate, active) VALUES (:post_id, :idLogged, :comment, 0, 0, :star, NOW())";
$result = $conexao->prepare($insert);
$result->bindParam(':post_id', $post_id, PDO::PARAM_INT);
$result->bindParam(':idLogged', $idLogged, PDO::PARAM_INT);
$result->bindParam(':star', $star, PDO::PARAM_INT);
$result->bindParam(':comment', $comment, PDO::PARAM_STR);
$result->execute();
if($result->rowCount() == 0){
echo '<div class="alert alert-success" role="alert">
<strong>Sucesso!</strong> avaliação cadastrada.
</div>';
}
else
{
echo '<div class="alert alert-danger">
<strong>Erro ao cadastrar!</strong> Não foi possível cadastrar a avaliação.
</div>';
}
}
}//if
What’s the matter?
– Maniero
@bigown N appeared no PHP error, only I have no record and is showing the Error message when registering
– William Alvares
There are two mistakes when registering, which one?
– rray
The error of 'if($result->rowCount() >= 1){'
– William Alvares
does not have logic the conditional structures and also erroneous use of rowCount() in Insert.
– Daniel Omine
The error must be SQL, have it printed to see what it is. Is it duplicate key? Anyway this logic is wrong, can provoke a racing condition. You don’t have to check anything, you have to enter, if you fail you should take a stand.
– Maniero
If select returns 2 or more records, which one is updated?
– rray
Sorry, I voted to close as "not clear enough" after observing ambiguities and details not well informed in the question, expressed in the comments. Describe the question better. Describe clearly what the flow rules are.
– Daniel Omine
@Danielomine It worked already, it was Make a SELECT > INSERT or UPDATE
– William Alvares
@Williamalvares is not what worked, it seems to work, it’s different. Right is something else.
– Maniero
^.- What do you mean, look at the code down there
– William Alvares