dongzheng7165 2015-10-23 12:33
浏览 17

MySQL准备好的声明似乎正确但不起作用

For practice, I'm trying to write a simple login code for a table that consists of three fields: id, teamnname and score.

There is a db_connect.php file that files that require it include at the top. I know this is working correctly (as other functions in the functions.php file appear to be working). This file defines $mysqli, which seems to be working fine elsewhere.

This file looks like:

<?php 
$mysqli = mysql_connect('host', 'database', 'password'); 
ob_start();
?> 

However, the only function that isn't working is my process_login function. The login.php file will not output anything other than "Connection Successful" from the database connection working properly. It should either indicate "invalid request" or redirect the page.

Here is the login.php script using this function...

<?php

include_once 'db_connect.php';
include_once 'functions.php';

custom_session();

if (isset($_POST['teamname'])) {
    $teamname = $_POST['teamname'];

    if (process_login($teamname, $mysqli) == true) {
        // redirect to the right place
        header('Location: ../index.php?start_game');
    } else {
        // redirect to an error page
        header('Location: ../index.php?error1')
    }
} else {

    // the correct POST variables were not sent to this page.
    echo 'Invalid Request';
}

And here is the problematic function in functions.php:

function process_login($teamname, $mysqli) {
    if ($stmt = $mysqli->prepare("SELECT id, teamname, score FROM users WHERE     teamname = ? LIMIT 1")) {
        $stmt->bind_param('s', $teamname);
        $stmt->execute();
        $stmt->store_result();

        $stmt->bind_result($id, $teamname, $score);
        $stmt->fetch();

        if ($stmt->num_rows == 1) {
            $_SESSION['id'] = $id;
            $_SESSION['teamname'] = $teamname;
            $_SESSION['score'] = $score;
            return true;
        }
    }

    return false;
}

I've played with this and the prepared statement at the top seems to be the culprit. I've run that SELECT syntax through the database directly so I know that it can't be wrong. I'm stumped having tried many things and would appreciate some seasoned help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度