php - Multiple queries & LastInsertId -
how wrong query? can insert multiple queries that? can use lastinsertid
that?
$pdo = database::connect(); $dflt = 'default'; $query1 = "insert 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 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 created."; }else{ echo "unable create product."; } database::disconnect(); } catch(pdoexception $exception){ echo "error: " . $exception->getmessage(); }
i've been searching couldn't find how use both in query , expired solutions, not sure wrong.
edit: i'm starting think more query, if notice something..
javascript
$(document).on('submit', '#create-aluno-form', function() { // show loader img $('#loader-image').show(); // post data form $.post("registar.php", $(this).serialize()) .done(function(data) { // show create product button $('#create-aluno').show(); showproducts(); }); return false; });
most statement fails insert, code full of problems:
- you used prepare statement yet put values in query string
- hashed_password undefined in first query
- you try bind multiple queries @ once
- wrong order prepare first query, execute , bind parameters -
$pdo->lastinsertid();
enough not sure why pass"utilizador"
try approach:
try{ $pdo = database::connect(); $dflt = 'default'; $hashed_password = hash( 'sha512', $_post['password']); $query1 = "insert 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 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(); }
Comments
Post a Comment