下面的代码是我用来提交表单数据的代码。表单提交得很好并且重定向没有问题。
当我查看数据库中的行数据时,我注意到 companyname、firstname 和 lastname 被插入为 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);
}
?>