Php $ var在HTML中没有返回任何值

时间:2018-03-25 00:47:09

标签: php

我将变量$success设置为$success = "Successfully Created",但var $success在HTML中没有值。

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // username and password sent from form

    if (isset($_POST['okbutton'])) {
        if (isset($_POST['clientuser'], $_POST['clientpass'])) {
            $clientuser = $_POST['clientuser'];
            $clientpass = $_POST['clientpass'];

            $_SESSION['cuser'] = $clientuser;
            $_SESSION['cpass'] = $clientpass;

            header('Location: trialaccount.php');
            die();
        }
    }
}
try {
    if (isset($_SESSION['cuser'])) {
        $stmt = $conn->prepare("SELECT user_id FROM user WHERE user_id=:username");
        $stmt->bindParam(':username', $_SESSION['cuser']);
        $stmt->execute();

        $checkdup = $stmt->rowCount();

        if ($checkdup == 0) {
            $stmt = $conn->prepare("INSERT INTO user (user_id, user_pass, user_online, user_enable, user_start_date, user_end_date, reseller, type) VALUES (:clientuser, :clientpass,0, 1, now(), now() + interval 4 hour, :panelUSER, 'Trial')");

            $stmt->bindParam(':clientuser', $_SESSION['cuser']);
            $stmt->bindParam(':clientpass', $_SESSION['cpass']);
            $stmt->bindParam(':panelUSER', $username);
            $stmt->execute();


            $success =  "Trial Account Created Successfully!";
        } else {
            $error = "Username '" . $_SESSION['cuser'] . "' is already taken. Try to input unique username." ;
        }
    }
} catch (PDOException $e) {
    echo "Error: Database Error";
}

在我的HTML中,我使用echo!

<?php if(isset($success)){ echo $success; } ?>

var $success正在我的个人智能手机上返回值, 但在其他设备上没有价值。

我不知道发生了什么事? 我可以使用Session吗? TY

1 个答案:

答案 0 :(得分:0)

您的方法无法获得总行数。因此,它不会通过( if($ checkdup == 0))来设置$ success的值。您可以尝试以下代码。

<强>替换

$stmt = $conn->prepare("SELECT user_id FROM user WHERE user_id=:username");
$stmt->bindParam(':username', $_SESSION['cuser']);
$stmt->execute();

$checkdup = $stmt->rowCount();

。通过

$stmt = $conn->prepare("SELECT COUNT(*) FROM user WHERE user_id=:username");
$stmt->bindParam(':username', $_SESSION['cuser']);
$stmt->execute();

$checkdup = $stmt->fetchColumn();