MySQLI从数据库中回显数据

时间:2016-11-14 19:45:08

标签: php mysqli

我正在创建一个网站,我正在使用mySqli连接到数据库。出于某种原因,当我尝试回显数据库中的信息时,我没有得到任何返回。我需要它从表中取一列并在页面上显示,这不会发生。我问别人,他们认为这很奇怪(我们花了大约1个小时试图解决它)所以任何帮助都会非常感激。

postimg.org/image/qqv4vmtf7 postimg.org/image/rp6hhz8ht

<?php
    ob_start();
    session_start();
    include_once 'dbconnect.php';

    if(!isset($_SESSION['user'])) {
        header("Location: index.php");
        exit;
    }

    $condition = empty($_POST['sender']) || empty($_POST['reciever']);
    if (!$condition) {
        $name = $_POST['sender'];       
        $reciever = $_POST['reciever'];

        $query = "UPDATE users SET userCoins = userCoins + 1  WHERE userName='Morgan'";
        $res = $mysqli->query($query);
        if ($res) {
            $error = "Success!";
        } else {
            $error = "Something Went Wrong!";
            echo "Error: ".$mysqli->error; // here you can check your errors
        }
        $sql= "SELECT * FROM users WHERE userId=".$_SESSION['user']; 
        $result = $mysqli->query($sql);
        $userRow = $result->fetch_assoc();
if (!$userRow) {
    die("userID {$_SESSION['user']} not found!");
}
    }
?>
<!DOCTYPE html>
<html>
    <?php header("Access-Control-Allow-Origin: http://www.py69.esy.es"); ?>
    <head>
        <title>ServiceCoin</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css"  />
        <link rel="stylesheet" href="scripts/home/index.css" />
    </head>
    <body>
        <ul>
            <li><a href="#" class="a">ServiceCoin.com(image)</a></li>
            <li><a href="logout.php?logout" class="a">Sign Out</a></li>
            <li><a href="#" class="a">Contact</a></li>
            <li><a href="#" class="a">Get Service Coins</a></li>
            <li><a href="#" class="a">News</a></li>
            <li><a href="settings.php" class="a">Settings</a></li>
            <li><a href="#" class="a">Referrals</a></li>
            <li><a href="service.php" class="a">Services</a></li>
            <li><a href="home.php" class="a">Home</a></li>
        </ul>
        <br /><br />
        <center>
        <h3>Welcome, <?php echo $userRow['userName']; ?>. You Currently Have <br /><br /><br /><br /><br /><span id="services"><?php var_dump($userRow) ?></span> Service Coins</h3>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off">
            <div class="form-group">
                <div class="input-group">
                    <span class="input-group-addon"><span class="glyphicons glyphicons-lock"></span></span>
                    <input type="text" name="sender" class="form-control" placeholder="Enter Your Wallet Key" maxlength="15" />
                    <span class="text-danger"><?php echo $error; ?></span>
                </div>
                <div class="input-group">
                    <span class="input-group-addon"><span class="glyphicons glyphicons-lock"></span></span>
                    <input type="text" name="reciever" class="form-control" placeholder="Enter The Recievers Wallet Key" maxlength="15" />
                    <span class="text-danger"><?php echo $error; ?></span>
                </div>

            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-block btn-primary" name="send">Sign Up</button>
            </div>
        </form>
        </center>
    </body>
</html>
<?php ob_end_flush(); ?>

1 个答案:

答案 0 :(得分:2)

您没有执行SELECT查询。你需要:

$sql= "SELECT * FROM users WHERE userId=".$_SESSION['user']; 
$result = $mysqli->query($sql);

然后改变:

$result->fetch_assoc();

$userRow = $result->fetch_assoc();

然后,您可以使用$userRow['userName']$userRow['userCoins']之类的内容来显示有关用户的信息。

您还应该检查查询是否找到了任何内容,例如:

if (!$userRow) {
    die("userID {$_SESSION['user']} not found!");
}

执行SELECT查询并设置$userRow的代码不应位于if(!$condition)块中,以便您在提交之前查看页面时会看到用户信息形式。