gpt4 book ai didi

php - SQL 提交 0 而不是实际值。还删除@和.在电子邮件地址中

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

下面的代码是我用来提交表单数据的代码。该表单提交得很好并且重定向没有问题。

当我查看数据库中的行数据时,我注意到公司名称、名字和姓氏被插入为 0。我还注意到,当电子邮件地址插入数据库时​​,它会删除 @ 和 。从地址。

如有任何帮助,我们将不胜感激。

<?php
include("inc/conf.inc.php"); // Includes the db and form info.
if (!isset($_POST['submit'])) { // If the form has not been submitted.

} else { // The form has been submitted.
$companyname = form($_POST['companyname']);
$firstname = form($_POST['firstname']);
$lastname = form($_POST['lastname']);
$username = form($_POST['emailaddress']);
$password1 = md5($_POST['password1']); // Encrypts the password.
$password2 = md5($_POST['password2']);

if (($companyname == "") || ($firstname == "") || ($lastname == "") || ($username == "") || ($password1 == "") || ($password2 == "")) { // Checks for blanks.
exit("There was a field missing, please correct the form.");
}

$q = mysql_query("SELECT * FROM `users` WHERE emailaddress = '$username'") or die (mysql_error()); // mySQL Query
$r = mysql_num_rows($q); // Checks to see if anything is in the db.

if ($r > 0) { // If there are users with the same username/email.
exit("That email address is already in use!");
} else {
mysql_query("INSERT INTO `users` (companyname,firstname,lastname,emailaddress,password) VALUES ('$companyname','$firstname','$lastname','$username','$password1')") or die (mysql_error()); // Inserts the user.
header("Location: thankyou.php"); // Back to login.
}
}
mysql_close($db_connect); // Closes the connection.
?>




<?php
$db_user = ""; // Username
$db_pass = ""; // Password
$db_database = ""; // Database Name
$db_host = "localhost"; // Server Hostname
$db_connect = mysql_connect ($db_host, $db_user, $db_pass); // Connects to the database.
$db_select = mysql_select_db ($db_database); // Selects the database.

function form($data) { // Prevents SQL Injection
global $db_connect;
$data = preg_replace('/[^A-Za-z0-9_]/', '', $data);
$data = mysql_real_escape_string(trim($data), $db_connect);
return stripslashes($data);
}
?>

最佳答案

如果您注意到,您提到的字段是由函数 form() 处理的。您可能想要打印它们以检查 form() 函数的返回值。

此外,代替支票,

  ($companyname == "")

使用:

  empty($companyname)

它检查的不仅仅是空字符串。

The following things are considered to be empty:

"" (an empty string)

0 (0 as an integer)

0.0 (0 as a float)

"0" (0 as a string)

NULL

FALSE

array() (an empty array)

$var; (a variable declared, but without a value)

关于php - SQL 提交 0 而不是实际值。还删除@和.在电子邮件地址中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12811267/

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