2017-06-03 108 views
-2

我有一個PHP腳本,用於將數據發送到我的數據庫。如果SQL語句執行成功我想送一個UPDATE語句看起來像:如果第一條語句被成功執行,請運行第二條語句

UPDATE accounts SET status='1' WHERE user_id='" . $_SESSION['user_id'] ."' 

是否有可能運行一個第二份聲明,如果成功地執行的第一條語句?如果是,那我該怎麼做?

這裏是我使用的數據發送到數據庫的腳本:如果兩個SQL語句執行成功,我想給用戶發送到頁面../success.php?id='.$id

編輯1

<?php 
session_start(); 
if (isset($_GET['id'])) 

$correct = true; 

$firstname = $_POST['firstname'] ; 
$lastname = $_POST['lastname'] ; 
$_SESSION['user_id']; 
$status = $_POST['status'] ; 

if($correct){ 
    $db = new PDO('mysql:host=localhost;dbname=db', 'root', ''); 

$query = "INSERT INTO users(firstname, lastname, user_id, status) VALUES (?, ?, ?, ?)"; 
$stmt = $db->prepare($query); 
$stmt->execute(array($firstname, $lastname, $_SESSION['user_id'], $status)); 

$id = $db->lastInsertId(); 
      header('Location: ../success.php?id='.$id); 
}else{ 
      header('Location: ../error.php'); 
} 
?> 

$query1 = "INSERT INTO users(firstname, lastname, user_id, status) VALUES (?, ?, ?, ?)" ; 
$stmt = $db->prepare($query1); 
$stmt->execute(array($firstname, $lastname, $_SESSION['user_id'], $status)); 


// the below code will execute if your insertion is successful 
$query2 = "UPDATE accounts SET status='1' WHERE user_id='" . $_SESSION['user_id'] ."'" ; 

// set the header location to go to another page 


$id = $db->lastInsertId(); 
      header('Location: ../success.php?id='.$id); 
}else{ 
      header('Location: ../error.php'); 
} 
+0

做u試圖執行更新查詢? –

+0

是的,更新查詢正在工作 – John

+0

我看不到這裏的$ query2-> execute()。 –

回答

-1

可以。使用的$statement->execute();

if ($statement->execute()) { 
    // next statement 
} 

返回值瞭解更多關於這在documentation

錯誤處理

除了上面的例子中,你可以檢查錯誤代碼。某些事情肯定會在某個時候出錯,所以您將需要執行一些錯誤處理。檢查錯誤代碼並決定相應的操作。

一個小例子:

if ($statement->errorCode() === "00000") { 
    // everything went well! 
} else { 
    throw new Exception("Something went wrong!"); 
} 

瞭解更多關於在documentation爲好。

+0

小心告訴我我的答案有什麼問題? –

+0

我試過你的代碼。校長也是一樣。當我輸入SQL語句時,它不起作用 – John

+0

那麼如果是這樣的話,請給我們更多有關什麼不起作用的細節。只是說它不起作用不會幫助我們幫助你。 –

0

$stml->execute()成功返回true。因此,U可以檢查

if($correct){ 
     $db = new PDO('mysql:host=localhost;dbname=db', 'root', ''); 

     $query = "INSERT INTO users(firstname, lastname, user_id, status) VALUES (?, ?, ?, ?)"; 
     $stmt = $db->prepare($query); 
     $exec = $stmt->execute(array($firstname, $lastname, $_SESSION['user_id'], $status)); 

     if($exec){ 
      ....do update query 
      // set the header location to go to another page 
     } 
    } 

或者乾脆你可以做到這一點

if($correct){ 
     $db = new PDO('mysql:host=localhost;dbname=db', 'root', ''); 

     $query = "INSERT INTO users(firstname, lastname, user_id, status) VALUES (?, ?, ?, ?)"; 
     $stmt = $db->prepare($query); 
     $exec = $stmt->execute(array($firstname, $lastname, $_SESSION['user_id'], $status)) or die(); 

     // the below code will execute if your insertion is successful 
     ....do update query 
     // set the header location to go to another page 
    } 
+0

它不起作用。在我的第一篇文章中看到「編輯1」 – John

+0

我的upvote。你的答案實際上是可行的。不明白爲什麼這是downvoted。 –