dougu5886 2015-06-19 04:41
浏览 204
已采纳

多个查询和LastInsertId

How wrong is that query? Can I insert multiple queries like that? Can I use lastInsertId like that?

$pdo = Database::connect();
    $dflt = 'DEFAULT';

    $query1 = "INSERT INTO utilizador(email, pass, nome, dt_registo, tipo, activo) 
                  VALUES (:email, '$hashed_password', :nome, :dt_registo, :tipo, :activo)";
    $stmt = $pdo->prepare($query1);
    $stmt->execute();
    $insertedid = $pdo->lastInsertId("utilizador");

    $query2 ="INSERT INTO aluno(morada, cd_postal, cidade, utilizador_id)
                VALUES (:morada, :cpostal, :cidade,'$insertedid')";

    $stmt2 = $pdo->prepare($query2);
    $stmt2->execute();

    $hashed_password = hash( 'sha512', $_POST['password']);
    $stmt->bindParam(':email',$_POST['email']);
    $stmt->bindParam(':nome',$_POST['nome']);
    $stmt->bindParam(':dt_registo',$dflt);
    $stmt->bindParam(':tipo',$dflt);
    $stmt->bindParam(':activo',$dflt);
    $stmt->bindParam(':morada',$_POST['morada']);
    $stmt->bindParam(':cpostal',$_POST['cpostal']);
    $stmt->bindParam(':cidade',$_POST['cidade']);

    if($stmt->execute()){        
        echo "Product was created.";
    }else{
        echo "Unable to create product.";
    }
    Database::disconnect();
}
catch(PDOException $exception){
    echo "Error: " . $exception->getMessage();
}

I've already been searching but couldn't find how to use both in a query and I already expired all the solutions, not sure which is wrong.

EDIT: I'm starting to think its more than the query, if someone notice something..

JAVASCRIPT

$(document).on('submit', '#create-aluno-form', function() {
    // show a loader img
    $('#loader-image').show();

    // post the data from the form
    $.post("registar.php", $(this).serialize())
        .done(function(data) {           
            // show create product button
            $('#create-aluno').show();
            showProducts();
        });
    return false;
});
  • 写回答

6条回答 默认 最新

  • dongzhi4470 2015-06-19 16:53
    关注

    Most likely your statement fails to insert, Your code is full of problems:

    • You used prepare statement but yet you put values in the query string
    • hashed_password is undefined in the first query
    • You try to bind multiple queries at once
    • wrong order prepare the first query, execute , then bind the parameters -$pdo->lastInsertId(); is enough not sure why you pass "utilizador"

    Try this approach:

    try{
        $pdo = Database::connect();
        $dflt = 'DEFAULT';
        $hashed_password = hash( 'sha512', $_POST['password']);
    
        $query1 = "INSERT INTO utilizador(email, pass, nome, dt_registo, tipo, activo) 
                      VALUES (:email, :pass, :nome, :dt_registo, :tipo, :activo)";
        $stmt = $pdo->prepare($query1);
        $stmt->bindParam(':email',$_POST['email']);
        $stmt->bindParam(':pass',$hashed_password);
        $stmt->bindParam(':nome',$_POST['nome']);
        $stmt->bindParam(':dt_registo',$dflt);
        $stmt->bindParam(':tipo',$dflt);
        $stmt->bindParam(':activo',$dflt);
        if($stmt->execute()){
            //query1 success
            $insertedid = $pdo->lastInsertId();
            $query2 ="INSERT INTO aluno(morada, cd_postal, cidade, utilizador_id)
                      VALUES (:morada, :cpostal, :cidade, :utilizador_id)";
            $stmt2 = $pdo->prepare($query2);
            $stmt2->bindParam(':morada',$_POST['morada']);
            $stmt2->bindParam(':cpostal',$_POST['cpostal']);
            $stmt2->bindParam(':cidade',$_POST['cidade']);
            $stmt2->bindParam(':utilizador_id',$insertedid);
            if($stmt2->execute()){        
                //query2 success
            }else{
                //query2 failed
            }
        }else{
                //query1 failed
        }
    
        Database::disconnect();
    }
    catch(PDOException $exception){
        echo "Error: " . $exception->getMessage();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件