为什么这个PHP代码访问MySQL数据库不起作用?

时间:2014-05-16 19:50:48

标签: php mysql

<?php

require('database.php');
$user = $_POST["username"];
    $password = $_POST["password"];
    $location = $_POST["location"];

    $stmt = $mysqli->prepare("insert into Userinfo (username, password, location) values (?, ?, ?)");
    if(!$stmt) {
        //printf("Query prep failed: %s\n", mysqli->error);
        echo "query prep failed".$mysqli->error;
        exit;
    }
    $stmt->bind_param('sss', $username, $password, $location);
    $stmt->execute();
    $stmt->close();
    error_log("username ".$user, 3, "/tmp/php_error.log");
} 
?>

database.php中

<?php

$mysqli = new mysqli('localhost', 'php', 'passtheword', 'Android');

if($mysqli->connect_errno) {
printf("Connection Failed: %s\n", $mysqli->connect_error);
exit;
}
?>

由于某种原因,此查询未修改我的数据库。我知道&#39; database.php&#39;是有效的,我不会从if(!$ stmt)部分收到错误。没有什么打破,它只是没有修改表,Userinfo。谁能告诉我为什么?

1 个答案:

答案 0 :(得分:3)

$user更改为$username。您绑定并插入$username,但只有$_POST并定义$user