php:if语句测试db值是否等于一个数字-if为真执行多个sql查询

gcuhipw9  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(227)

您好,我正在尝试生成php代码,只要某个数据库值等于1,就可以执行多个sql查询。如果该值不等于1,则将页面重定向到oops.php。
以下是我目前的代码:

<?php
session_start();

$servername = "localhost";
$username = "myUser";
$password = "myPass";
$dbname = "cashball_accounts";
$cash_amount = $_SESSION['cash_amount'];

// Create connection

$userid = $_SESSION['id'];

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Fetch the existing value of the cash_amount against that particular user here. You can use the SELECT cash_amount from users where userid = $userid

$_SESSION['cash_amount'] += $_POST['cashmade'];

$sql = "UPDATE users SET cashincheck = 0 WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $userid);
$result = $stmt->execute();

if($result)
{
   echo "cashin complete!";
}
else
{
   echo mysqli_error($conn);
   session_start();
   session_unset();
   session_destroy();
}

$conn->close();
?>

因此,如果数据库中的变量“cashincheck”等于1,我希望从//fetch注解到if($result)的所有内容都执行。
例如:

if(SELECT cashincheck FROM users WHERE id = ? = 1) {

$_SESSION['cash_amount'] += $_POST['cashmade'];

$sql = "UPDATE users SET cashincheck = 0 WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $userid);
$result = $stmt->execute();

    } else {
//redirect to oops.php
}

/\我知道这根本行不通这只是个例子/

我还想做几个其他的if语句并相应地更新数据库,这意味着需要更多的sql查询和if语句,那么如何添加更多的语句呢?
单独if语句的另一个示例:

if($_POST['cashmade'] < $_POST['type']) {

    $sql = "UPDATE users SET moneymade = moneymade + $_POST['cashmade'] WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('s', $userid);
    $result = $stmt->execute();

} else {

    $sql = "UPDATE users SET moneylost = moneylost + $_POST['type'] - $_POST['cashmade'] WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('s', $userid);
    $result = $stmt->execute();
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题