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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度