gpt4 book ai didi

php - 多个查询和 LastInsertId

转载 作者:行者123 更新时间:2023-11-29 12:09:31 26 4
gpt4 key购买 nike

这个查询有多错?我可以像这样插入多个查询吗?我可以像这样使用 lastInsertId 吗?

$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();
}

我已经在搜索,但找不到如何在查询中使用这两个解决方案,并且所有解决方案都已过期,不确定哪个是错误的。

编辑:我开始认为它不仅仅是查询,如果有人注意到某些事情..

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;
});

最佳答案

很可能你的语句插入失败,你的代码充满了问题:

  • 您使用了准备语句,但仍将值放入查询字符串中
  • hashed_pa​​ssword 在第一个查询中未定义
  • 您尝试一次绑定(bind)多个查询
  • 错误的顺序准备第一个查询,执行,然后绑定(bind)参数-$pdo->lastInsertId(); 足以不确定为什么您传递 "utilizador"
<小时/>

尝试这种方法:

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();
}

关于php - 多个查询和 LastInsertId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30930017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com