在不同的if语句中回显两件事

时间:2014-04-27 12:47:02

标签: php if-statement

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
    $serv_id = $_POST['serv_id'];
    $time    = $_POST["time"];
    $date    = $_POST["date"];

    if (!isset($_SESSION['id'])) 
    {
        echo "sorry you're not logged in.";
        exit();
    }

    if(date('w', strtotime($date)) == 6 || date('w', strtotime($date)) == 0) 
    {
        echo 'Event is on a weekend and cannot be booked.'; 
    } 
    else 
    {
        echo 'Thank for you booking with Claires hair and beauty';
        $time = $time. ":00:00";

        $result = mysqli_query($con, "SELECT time FROM tbl_booking WHERE time = '$time' AND date = '$date'") or trigger_error("Query Failed! SQL: $result - Error: ".mysqli_error($con), E_USER_ERROR);
    }

    if(mysqli_num_rows($result) == 0) 
    {
        $sql = "INSERT INTO tbl_booking (tbl_mem_id, serv_id, date, time) VALUES ('{$_SESSION['id']}','$serv_id','$date','$time')";
        mysqli_query($con, $sql) or die('Error: ' . mysqli_error($con));
        location: 'dashboard.php';
    } 
    else 
    {
        echo("this time is already booked");
    }
}

基本上我试图通过检查会话变量来检查他们是否已登录,然后检查他们输入的日期是否是周末,然后输入的日期/时间是否已被视为它只需要回显this time is already booked

但是发生的事情是什么时候已经采取了这个插槽?它回声

Thank for you booking with Claires hair and beautythis time is already booked

1 个答案:

答案 0 :(得分:2)

试用此代码:

if(date('w', strtotime($date)) == 6 || date('w', strtotime($date)) == 0) 
    {
        echo 'Event is on a weekend and cannot be booked.'; 
    } 
    else 
    {

        $time = $time. ":00:00";

        $result = mysqli_query($con, "SELECT time FROM tbl_booking WHERE time = '$time' AND date = '$date'") or trigger_error("Query Failed! SQL: $result - Error: ".mysqli_error($con), E_USER_ERROR);
    }

    if(mysqli_num_rows($result) == 0) 
    {
        $sql = "INSERT INTO tbl_booking (tbl_mem_id, serv_id, date, time) VALUES ('{$_SESSION['id']}','$serv_id','$date','$time')";
        mysqli_query($con, $sql) or die('Error: ' . mysqli_error($con));
        echo 'Thank for you booking with Claires hair and beauty';//this is right place
        location: 'dashboard.php';// Don't know what you trying to do here
    } 
    else 
    {
        echo("this time is already booked");
    }

如果你想重定向到php中的另一个页面,请使用:

header('Location: yourpage.php');

PHP Header Function