使用MySQL的预备语句不将POST值插入数据库表

时间:2017-03-28 20:16:57

标签: php mysql database prepared-statement

说实话我会说我是新手编码员,他知道基本知识,但正在努力学习更多,这个问题也是我创建帐户的原因,而且它真的难倒了我。我的很多代码都是暂时的,我计划稍后简化它,并添加星号替换密码输入等功能。

我的代码的期望结果是下面的变量值应该与我的数据库表中的值进行比较,具体取决于$ type的值。我遇到的问题是没有条目添加到我的数据库表中。我不确定问题在我的代码中的位置,我可以用正确的方向做一点,这是我第一次准备好的语句,所以我可能会错误地使用它们

主要剧本:

<?php

include connect.db;

//These are normally user inputs from a form in another file.
$type = "students";
$username = "usernametest";
$password = "passwordtest";
$email = "emailtest";

//the connect global initilizes a connection between my database.
$query = $GLOBALS["conn"]->query("SELECT * FROM '$type' WHERE (username = '$username') OR (password = '$password') OR (email = '$email')");
if (mysqli_num_rows($query) == false) {
    $stmt = $GLOBALS["conn"]->prepare("INSERT INTO ? (username, password, email) VALUES (?,?,?)");
    $stmt->bind_param("ssss", $type, $username, $password, $email);
    $stmt->execute();
    $stmt->close();
    echo "User Registered";
}   
else {
    echo "Username, password or email are already used";
}

?>

连接脚本:

<?php
//Identifies the databases details.

//Identifies the servername the database is created in
$servername = "localhost";
//Identifies the databases username
$username = "htmltes7";
//Identifies the database password
$password = "(mypassword)";
//Identified the afformentioned databasename
$dbname = "htmltes7_dbname";

/*Creates a new global variable which opens a connection between my database 
using the variables above */
$GLOBALS["conn"] = new mysqli($servername, $username, $password, $dbname);
/*IF the connection cannot be made then the equilivent of exit() occours
in the form of die(). An error message is displayed containing information
on the error that occoured using mysqli_connect_error.*/
if (!$GLOBALS["conn"]) {
    die("Connection failed: " . mysqli_connect_error());
}

?>

编辑:抱歉我的格式不正确和第一次使用不正确的标签,就像我说我对sql和堆栈溢出都是新手一样,有点跳过枪来问我的问题。我已经根据反馈进行了更改,并且未来不会重现相同的错误。

1 个答案:

答案 0 :(得分:0)

尝试查看错误

error_reporting(E_ALL);
ini_set('display_errors', 1);



if (!$stmt) {
    echo "\nPDO::errorInfo():\n";
    print_r($dbh->errorInfo());
}