gpt4 book ai didi

PHP 注册脚本,插入数据库不起作用

转载 作者:行者123 更新时间:2023-11-30 01:06:16 26 4
gpt4 key购买 nike

我的网站上的注册表单 PHP 代码有问题。提交表单时,某些代码不会执行。

if ($fnameError = "" && $emailError = "" && $userError = "" && $passError = "" && $cpassError = "" && $tickError = "") {
$con = mysqli_connect("localhost","root","","login")or die("Can't connect to database");

$user = $_POST['user'];
$pass = $_POST['pass'];
$name = $_POST['fname'];
$email = $_POST['email'];

$pwd = crypt('$pass', '$user');
$pwd = md5($pwd);
$tblname = "users";
$flp="INSERT INTO $tblname (Name, Username, Password, Email Address)
VALUES
('$name','$user','$pass','$email')";
$res = mysqli_query($con, $flp)or die("Can't insert to table");
if ($res) {
$complete = "Registered successfully please log in to continue";
} else {
echo "error";
}
}

一切正常,直到到达以 $flp="INSERT INTO... 开头的行

任何人都可以帮助我调试此代码吗?另外,请不要链接到已经编写的代码,我希望能够使用此代码。

编辑:

我更改了一行故意导致错误,因此我知道 PHP 正在读取该行,并且它确实给了我以 $res=mysqli_... 开头的行的语法错误

“解析错误:语法错误,第 85 行 C:\XamppNew\htdocs\site\regusr.php 中出现意外的 '$res' (T_VARIABLE)”

我删除了插入行末尾的分号只是为了得到错误。

编辑:

我已经设法将问题隔离到 if 语句的开头。该行似乎不会将每个错误视为没有内容。但是,如果存在错误,它将显示在表单旁边的页面上,并且不会显示此类错误。

最佳答案

试试这个:

<?php

//if ($fnameError = "" && $emailError = "" && $userError = "" && $passError = "" && $cpassError = "" && $tickError = "")
//{
// Connect to DB
$mysqli = new mysqli("localhost", "root", "", "login");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}

// Parse Input
$user = $mysqli->real_escape_string($_POST['user']);
$pass = $mysqli->real_escape_string($_POST['pass']);
$pwd = md5(crypt('$pass', '$user'));
$name = $mysqli->real_escape_string($_POST['fname']);
$email = $mysqli->real_escape_string($_POST['email']);

// Insert Record
if ($mysqli->query("INSERT INTO users (`Name`, `Username`, `Password`, `Email Address`) VALUES ('$name', '$user', '$pwd', '$email')")) {
printf ("New user has id %d.\n", $mysqli->insert_id);
} else {
printf("Failed to insert row: %s\n", $mysqli->error);
}

// Close DB Connection
$mysqli->close();
//}

?>

关于PHP 注册脚本,插入数据库不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19723980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com