gpt4 book ai didi

php - 为什么PHP在mysql表数据库中插入变量名

转载 作者:行者123 更新时间:2023-11-29 13:10:23 25 4
gpt4 key购买 nike

我正在制作 PHP 登录/注册表单。

我成功登录、注册和配置并且它有效,但我真的不明白为什么我的代码将变量名称 "email1""pass1" 插入到表而不是我输入的内容?

请帮忙,我真的看了大约 50 遍,看不出有什么问题。

这是我的注册表格:

    <?php
require ('config.php');

if(isset($_POST['submit'])){

//Perform the verification

$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];

if($email1 == $email2){
if($pass1 == $pass2){
//All good carry on

$name = mysql_escape_string($_POST['name']);
$lname = mysql_escape_string($_POST['lname']);
$uname = mysql_escape_string($_POST['uname']);
$email1 = mysql_escape_string('email1');
$email2 = mysql_escape_string('email2');
$pass1 = mysql_escape_string('pass1');
$pass2 = mysql_escape_string('pass2');



$sql = mysql_query("SELECT * FROM `users` WHERE `uname` = '$uname'");
if (mysql_num_rows($sql) > 0){
echo "That user already exists";
exit();
}


mysql_query("INSERT INTO `users`(`id`, `name`, `lname`, `uname`, `email`, `pass`) VALUES (NULL, '$name', '$lname', '$uname', '$email1', '$pass1')") or die(mysql_error());

}else{
echo "Sorry, your passwords do not match. <br />";
exit();
}
}else{
echo "Sorry your emails do not match. <br />";
}





}else{

$form = <<<EOT
<form action="register.php" method="POST">
First Name: <input type="text" name="name" /><br />
Last Name: <input type="text" name="lname" /><br />
Username: <input type="text" name="uname" /><br />
Email: <input type="text" name="email1" /><br />
Confirm Email: <input type="text" name="email2" /><br />
Password: <input type="password" name="pass1" /><br />
Confirm password: <input type="password" name="pass2" /><br />
<input type="submit" value="Register" name="submit" />
</form>
EOT;

echo $form;

}

?>

这是我的登录表单:

<!--KODA ZA ŠUMNIKE-->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<?php
require ('config.php');

if(isset($_POST['submit'])){
$uname = mysql_escape_string($_POST['uname']);
$pass = mysql_escape_string($_POST['pass']);


$sql = mysql_query("SELECT * FROM `users` WHERE `uname` = '$uname' AND `pass` = '$pass'");
if(mysql_num_rows($sql) > 0){
echo "You are now logged in.";
exit();
}else{
echo "Wrong username or password combination.";
}

}else{

$form = <<<EOT
<form action="login.php" method="POST">
Username: <input type="text" name="uname" /><br />
Password: <input type="password" name="pass" /><br />
<input type="submit" value="Log in" name="submit" />
</form>
EOT;

echo $form;

}

最佳答案

              $name = mysql_escape_string($_POST['name']);
$lname = mysql_escape_string($_POST['lname']);
$uname = mysql_escape_string($_POST['uname']);
$email1 = mysql_escape_string($email1);
$email2 = mysql_escape_string($email2);
$pass1 = mysql_escape_string($pass1);
$pass2 = mysql_escape_string($pass2);

应该修复它。您正在转义字符串“email1”等。

还希望迁移到 mysqli 或 pdo 而不是旧的 mysql 方法

关于php - 为什么PHP在mysql表数据库中插入变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22174449/

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