Kamata Kamata - 2 days ago 4
MySQL Question

Update record then delete

I'm trying to update one record and then delete another in one go, however it's only allowing me to do one or the other;

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


How do I get it to do both?
I have tried the following;

$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'";


__

FULL CODE:

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


}

Answer

You Try change in Your full code Like this

in your Execute code


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