Php 为什么我的代码不将详细信息插入数据库?
我已经编写了以下代码将数据插入数据库表,但不幸的是,它不起作用。我已经仔细检查了代码,没有任何错误。控制台也没有显示任何内容 我没有拼错连接数据库所需的任何细节,而且变量也正确无误,因此我不知所措 如果有人能帮忙,我们将不胜感激 我的代码:Php 为什么我的代码不将详细信息插入数据库?,php,mysql,Php,Mysql,我已经编写了以下代码将数据插入数据库表,但不幸的是,它不起作用。我已经仔细检查了代码,没有任何错误。控制台也没有显示任何内容 我没有拼错连接数据库所需的任何细节,而且变量也正确无误,因此我不知所措 如果有人能帮忙,我们将不胜感激 我的代码: <?php function obfuscate($type, $data) { if ($type == "PIN"): $f_int = (int)md5($data); $data = str_split(
<?php
function obfuscate($type, $data) {
if ($type == "PIN"):
$f_int = (int)md5($data);
$data = str_split($data);
$rev = implode("", array_reverse($data));
$sum = array_sum($data) + $f_int;
$data = implode("", $data);
$data += $sum + $rev + 1026;
// Keep the first four digits if the result is longer
if (strlen($data) > 4):
$data = substr($data, 0, 4);
endif;
return $data;
elseif ($type == "password"):
$data = password_hash($data, PASSWORD_DEFAULT, ['cost' => 12]);
return $data;
endif;
}
function insert_user($username, $email, $password, $PIN, $Account_Type, $Account_Status, $Referrer, $Balance) {
// Connect to the server and the database or show error
$connection = mysqli_connect("localhost", "root", "") or die("Couldn't connect to the server.");
mysqli_select_db($connection, "Calisoft_flu_db") or die("Couldn't connect to the database.");
// Sanitise the data
$username = mysqli_real_escape_string($connection, $username);
$email = mysqli_real_escape_string($connection, $email);
$password = mysqli_real_escape_string($connection, $password);
$PIN = mysqli_real_escape_string($connection, $PIN);
// Get the rest of the data
$Registration_Date = date("Y-m-d");
// Obfuscate password and PIN
$password = obfuscate("password", $password);
$PIN = obfuscate("PIN", $PIN);
// Make query and insert data to database
$query = "INSERT INTO `users` (`ID`, `Username`, `Email`, `Password`, `PIN`, `Registration_Date`, `Account_Type`, `Account_Status`, `Referrer`, `Balance`) VALUES ('NULL', '$username', '$email', '$password', '$PIN', '$Registration_Date', '$Account_Type', '$Account_Status', '$Referrer', '$Balance')";
$registered = mysqli_query($connection, $query);
if ($registered) {
echo "Register successful!";
}
// End the connection
mysqli_close($connection);
}
insert_user("@user1", "user@gmail.com", "user12345678", "1234", "Member", "Active", "0", "0");
?>
如果要在主键中插入“NULL”字符串值,请删除引号“”或从查询中省略ID
删除引号:
$query = "INSERT INTO `users` (`ID`, `Username`, `Email`, `Password`, `PIN`, `Registration_Date`, `Account_Type`, `Account_Status`, `Referrer`, `Balance`) VALUES (NULL, '$username', '$email', '$password', '$PIN', '$Registration_Date', '$Account_Type', '$Account_Status', '$Referrer', '$Balance')";
$query = "INSERT INTO `users` ( `Username`, `Email`, `Password`, `PIN`, `Registration_Date`, `Account_Type`, `Account_Status`, `Referrer`, `Balance`) VALUES ('$username', '$email', '$password', '$PIN', '$Registration_Date', '$Account_Type', '$Account_Status', '$Referrer', '$Balance')";
或删除ID列:
$query = "INSERT INTO `users` (`ID`, `Username`, `Email`, `Password`, `PIN`, `Registration_Date`, `Account_Type`, `Account_Status`, `Referrer`, `Balance`) VALUES (NULL, '$username', '$email', '$password', '$PIN', '$Registration_Date', '$Account_Type', '$Account_Status', '$Referrer', '$Balance')";
$query = "INSERT INTO `users` ( `Username`, `Email`, `Password`, `PIN`, `Registration_Date`, `Account_Type`, `Account_Status`, `Referrer`, `Balance`) VALUES ('$username', '$email', '$password', '$PIN', '$Registration_Date', '$Account_Type', '$Account_Status', '$Referrer', '$Balance')";
如果要将字符串值“NULL”插入主键,请从查询中删除引号“”或省略ID
删除引号:
$query = "INSERT INTO `users` (`ID`, `Username`, `Email`, `Password`, `PIN`, `Registration_Date`, `Account_Type`, `Account_Status`, `Referrer`, `Balance`) VALUES (NULL, '$username', '$email', '$password', '$PIN', '$Registration_Date', '$Account_Type', '$Account_Status', '$Referrer', '$Balance')";
$query = "INSERT INTO `users` ( `Username`, `Email`, `Password`, `PIN`, `Registration_Date`, `Account_Type`, `Account_Status`, `Referrer`, `Balance`) VALUES ('$username', '$email', '$password', '$PIN', '$Registration_Date', '$Account_Type', '$Account_Status', '$Referrer', '$Balance')";
或删除ID列:
$query = "INSERT INTO `users` (`ID`, `Username`, `Email`, `Password`, `PIN`, `Registration_Date`, `Account_Type`, `Account_Status`, `Referrer`, `Balance`) VALUES (NULL, '$username', '$email', '$password', '$PIN', '$Registration_Date', '$Account_Type', '$Account_Status', '$Referrer', '$Balance')";
$query = "INSERT INTO `users` ( `Username`, `Email`, `Password`, `PIN`, `Registration_Date`, `Account_Type`, `Account_Status`, `Referrer`, `Balance`) VALUES ('$username', '$email', '$password', '$PIN', '$Registration_Date', '$Account_Type', '$Account_Status', '$Referrer', '$Balance')";
回显您的查询,并在mysql控制台中运行该查询并检查结果。您是否获得注册成功代码>?您还应该检查查询的状态。请。现在还为时过早,请学习参数化查询。错误是在“”中添加了NULL@PatrickGregorio,通过参数化查询,您的意思是像我们在PDO中那样准备语句并绑定参数(例如用户名)?这可以通过mysqli实现吗?我更喜欢使用mysqli。@AngelPolitis是的,对于mysqli,请尝试阅读,并在Echo上阅读您的查询,然后在mysql控制台中运行该查询并检查结果。您是否获得注册成功代码>?您还应该检查查询的状态。请。现在还为时过早,请学习参数化查询。错误是在“”中添加了NULL@PatrickGregorio,通过参数化查询,您的意思是像我们在PDO中那样准备语句并绑定参数(例如用户名)?这可以通过mysqli实现吗?我更喜欢使用mysqli。@AngelPolitis是的,对于mysqli,请尝试阅读,并且阅读此答案可能是因为假设ID
不是varchar
,或者任何基于字符串的列类型。你最好问问OP什么列类型ID
。@Marcus他是对的。在我的例子中,ID是一个自动递增的整数,他的解决方案非常有效。谢谢你,沙迪,好东西。仅供参考@AngelPolitis,在未来,提供尽可能多的相关信息是值得的。对于数据库查询,提供相关的表模式可以节省大量时间。很高兴你把它整理好了。谢谢@Marcus,事实上,我对我的问题的解释得更透彻一点。很好@Marcus,我应该先验证列类型。这个答案可能是因为假设ID
不是varchar
,也不是任何基于字符串的列类型。你最好问问OP什么列类型ID
。@Marcus他是对的。在我的例子中,ID是一个自动递增的整数,他的解决方案非常有效。谢谢你,沙迪,好东西。仅供参考@AngelPolitis,在未来,提供尽可能多的相关信息是值得的。对于数据库查询,提供相关的表模式可以节省大量时间。很高兴你把它整理好了。谢谢@Marcus,事实上,我必须更彻底地解释我的问题。很好@Marcus,我应该先验证一下列类型。