2016-12-01 145 views
0

我試圖更新一條記錄,然後一次刪除另一條記錄,但它只允許我做一個或另一個;更新記錄,然後刪除

$update = "UPDATE user SET felcredits = '$value' WHERE username = '$user'"; 
$update2 = "DELETE FROM playeritems WHERE id = '$realid'"; 

我該如何做到這一點? 我試過以下;

$update = "UPDATE user SET felcredits = '$value' WHERE username = '$user'"; 
$update = "DELETE FROM playeritems WHERE id = '$realid'"; 

__

$update = "UPDATE user SET felcredits = '$value' WHERE username = '$user' DELETE FROM playeritems WHERE id = '$realid'"; 

__

全碼:

if ($_SERVER['REQUEST_METHOD'] = $_POST AND isset($_POST['sell'])) { 
$sql = "SELECT felcredits FROM user WHERE username = '$user'"; 
$result = $db_conn->query($sql); 
if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     $felcredits = $row['felcredits']; 
    } 
} 
$value = $felcredits + $value; 
$update = "UPDATE user SET felcredits = '$value' WHERE username = '$user'"; 
$update2 = "DELETE FROM playeritems WHERE id = '$realid'"; 
if ($db_conn->query($update) === TRUE) { 
    echo "<br />Details Updated"; 
} else { 
    echo "Error: " . $insert . "<br>" . $db_conn->error; 
} 

}

+1

只需單獨執行即可 – Ghost

+0

顯示完整的代碼。 – markdwhite

+0

完整的代碼添加到底部 – Kamata

回答

0

你在完整的代碼嘗試改變像這樣

在執行代碼


if ($_SERVER['REQUEST_METHOD'] = $_POST AND isset($_POST['sell'])) { 
$sql = "SELECT felcredits FROM user WHERE username = '$user'"; 
$result = $db_conn->query($sql); 
if ($result->num_rows > 0) { 
// output data of each row 
while($row = $result->fetch_assoc()) { 
    $felcredits = $row['felcredits']; 
} 
}  
$value = $felcredits + $value; 
$update = "UPDATE user SET felcredits = '$value' WHERE username = '$user'"; 
$update2 = "DELETE FROM playeritems WHERE id = '$realid'"; 
if (($db_conn->query($update) === TRUE) && ($db_conn->query($update2) ===  TRUE)) { 
echo "<br />Details Updated"; 
} else { 
echo "Error: " . $insert . "<br>" . $db_conn->error; 
} 
} 
4

的問題是在這裏:

$update = "UPDATE user SET felcredits = '$value' WHERE username = '$user'"; 
$update = "DELETE FROM playeritems WHERE id = '$realid'"; 

你的第二個SQL查詢將覆蓋第一個,所以要單獨的查詢,如:

$update = "UPDATE user SET felcredits = '$value' WHERE username = '$user'"; 
mysqli_query($conn, $update); 

$delete = "DELETE FROM playeritems WHERE id = '$realid'"; 
mysqli_query($conn, $delete); 

其中$ conn是連接手柄

+0

這應該是公認的答案,而不是上面的答案。 – Ronald

+0

@Ronald好吧,可能是蒲田對上面的那個更舒服 –