代码之家  ›  专栏  ›  技术社区  ›  Nrc

与PDO和准备报表的交易

  •  -1
  • Nrc  · 技术社区  · 8 年前

    我需要插入两个表,我尝试了事务处理。它工作得很好:

    $nom = "Nrc";
    $contrasenya = "somePassword"; 
    
        $conn->beginTransaction();
    
        $conn->exec("INSERT INTO usuari (nom, contrasenya) 
                     VALUES ('$nom', '$contrasenya')");
        $conn->exec("INSERT INTO well (puntuacio, text) 
                     VALUES ('9', 'some text2')");
    
        $conn->commit();
        echo "New records created successfully";
    

    现在,我要介绍安全准备声明。我不知道该怎么做。这就是我所尝试的。它不会给我任何错误,但也不会在任何表中插入:

    $nom = "Nrc";
    $contrasenya = "somePassword"; 
    
        $conn->beginTransaction();
    
        $stmt = $conn->prepare("INSERT INTO usuari (nom, contrasenya) 
                                VALUES (:nom, :contrasenya)");
        $stmt = $conn->prepare("INSERT INTO well (puntuacio, text) 
                                VALUES ('9', 'some text2')");
    
        $stmt->bindParam(':nom', $nom);
        $stmt->bindParam(':contrasenya', $contrasenya);
        $conn->commit();
        echo "New records created successfully";
    
    3 回复  |  直到 8 年前
        1
  •  1
  •   Sebastian Brosch Navjyot    8 年前

    您的代码有几个问题:

    1. 你从来没有 execute 声明。
    2. 你改写了你的陈述( $stmt )直接使用值的语句。所以你没有使用正确的准备好的陈述。

    您可以使用以下代码 INSERT 表中的值:

    //start the transaction.
    $conn->beginTransaction();
    
    //the variables of the first statement.
    $nom = 'Nrc';
    $contrasenya = 'somePassword';
    
    //prepare the first statement, bind the values and execute.
    $stmt = $conn->prepare("INSERT INTO usuari (nom, contrasenya) VALUES (:nom, :contrasenya)");
    $stmt->bindParam(':nom', $nom);
    $stmt->bindParam(':contrasenya', $contrasenya); //TODO - use hashing here!
    
    //... or solution without variable.
    //$stmt->bindValue(':nom', 'Nrc');
    //$stmt->bindValue(':contrasenya', 'somePassword');
    
    $stmt->execute();
    
    //the variables of the second statement.
    $puntuacio = '9';
    $text = 'some text2';
    
    //prepare the second statement, bind the values and execute.
    $stmt = $conn->prepare("INSERT INTO well (puntuacio, text) VALUES (:puntuacio, :text)");
    $stmt->bindParam(':puntuacio', $puntuacio);
    $stmt->bindParam(':text', $text);
    
    //... or solution without variable.
    //$stmt->bindValue(':puntuacio', '9');
    //$stmt->bindValue(':text', 'some text2');
    
    $stmt->execute();
    
    //commit all changes of the transaction.
    $conn->commit();
    

    注: 正如其他人已经提到的,你也应该 hash your passwords .

        2
  •  -1
  •   Jojoes    8 年前

    php.net :

    与pdoStatement::bindValue()不同,变量作为引用绑定,并且只在调用pdoStatement::execute()时计算。

        3
  •  -1
  •   Snics    8 年前

    对于密码插入,应使用 password() function 与php一起发货。

    您不应该像在第一个语句中那样直接在PREPARE语句中插入数据。

    $stmt = $conn->prepare("INSERT INTO well (puntuacio, text) 
                            VALUES (:number, :some_text)");
    $stmt->bindParam(':number', $num);
    $stmt->bindParam(':some_text', $text);
    

    你应该 execute(); 为了执行查询插入而准备的语句。

    另外,如前所述,在执行查询之前,您会覆盖$stmt变量。