I want to make an insert only if there's no correspondence in the db (mySQL) but he makes me not the statement. Here's the snippet
if ($sql->rowCount() > 0) {
echo 'Non inserisci';
} else {
echo 'Inserisci';
$db->beginTransaction();
echo 'Ciao3';
$sql = $db->prepare("INSERT INTO contatti (nome,cognome) VALUES (?,?)") or die('Ciao2');
echo 'Ciao4';
$sql->execute(array($_POST['nome'],$_POST['cognome']));
echo 'Ciao5';
$db->rollBack();
}
Where The SELECT is
$db->beginTransaction();
$sql = $db->prepare("SELECT * FROM contatti WHERE nome = ? AND cognome = ? WHERE nome = ? AND cognome = ?") or die ('Ciao1');
$sql->execute(array($_POST['nome'],$_POST['cognome']));
$db->rollBack();
Can you explain me where's the fault?
The fault is in arithmetics
Let's count tokens:
SELECT * FROM contatti WHERE nome = ? // one
AND cognome = ? // two
WHERE nome = ? // three
AND cognome = ? // four
now let's count number of bound variables:
array($_POST['nome'], // one
$_POST['cognome']) // two
4 is apparently not equal to two. that's the problem