![](/img/trans.png)
[英]Inserting null values to database using prepared statements PHP MYSQL
[英]Prepared statements using MySQL not inserting POST values to database table
老實說我是一個菜鳥編碼者,他知道一些基礎知識,但想學更多,這也是我開賬的原因,也是讓我很沮喪的原因。 我的許多代碼都是臨時的,我打算稍后對其進行簡化,並添加諸如星號之類的功能來代替密碼輸入。
我的代碼的理想結果是,應根據$ 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和堆棧溢出都是陌生的,有點跳槍問我的問題。 我已經根據反饋進行了更改,以后不再重現相同的錯誤。
嘗試看看錯誤
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (!$stmt) {
echo "\nPDO::errorInfo():\n";
print_r($dbh->errorInfo());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.