3

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?

4

2 回答 2

13

错误在于算术

让我们计算令牌:

 SELECT * FROM contatti WHERE nome = ? // one
                       AND cognome = ? // two
                        WHERE nome = ? // three
                       AND cognome = ? // four

现在让我们计算绑定变量的数量:

array($_POST['nome'], // one
      $_POST['cognome']) // two

4显然不等于2。那就是问题所在

于 2013-04-19T15:48:29.890 回答
12

看来你在这里做了太多的复制/粘贴:

$sql = $db->prepare("SELECT * FROM contatti WHERE nome = ? AND cognome = ? WHERE nome = ? AND cognome = ?") or die ('Ciao1');

应该只是

$sql = $db->prepare("SELECT * FROM contatti WHERE nome = ? AND cognome = ?") or die ('Ciao1');

你已经把WHERE条款加倍了。

于 2013-04-19T15:47:50.470 回答