The below code is what I am using to submit my form data. The form submits just fine and redirects without issue.
When I look at the row data within the database, I notice that companyname, firstname, and lastname are being inserted as 0’s. I also noticed that, while the email address is being inserted into the database, it is dropping the @ and . from the address.
Any help would be appreciated.
<?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);
}
?>
So let me start from this 🙂
I’m sorry, but
Your code is extremely unorganized…
Let’s assume that your new config(or lib) file will be similar to this one:
File:
config.phpFile
handler.phpThis will work for you