PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi,
I am new to php. I was making this simple registration form for my website. Sadly, there are errors in this. Can some one please help me to find the error in this? When i run this script it gives me "database_error" near line 55.
I would like to search the database then find out if the user already exist and then continue with registration. Is there any provision for using "while()" in php, so that, i will do a similarity check between userid and what is already available in database.
<?php
//Continue the session
session_start();
//Make sure that the input come from a posted form. Otherwise quit immediately
if ($_SERVER["REQUEST_METHOD"] <> "POST")
die(header("Location:error_page.php"));
//Check if the security code and the session value are not blank
//and if the input text matches the stored text
if ( ($_REQUEST["txtCaptcha"] == $_SESSION["security_code"]) &&
(!empty($_REQUEST["txtCaptcha"]) && !empty($_SESSION["security_code"])) ) {
// Create database connection
$hostname='-';
$username='-';
$password='-';
$dbname='-';
$con = mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
// Check if the connection created
if(!$con){
die("Cannot connect. " . mysql_error());
}
// Check if the database selected
$dbselect = mysql_select_db($dbname);
if(!$dbselect) {
die("Cannot select database " . mysql_error());
}
// Get the user data
$name = $_REQUEST ['_name'];
$email = $_REQUEST ['_email'];
$password = $_REQUEST ['_password'];
// Check if there is any null values
if (empty($name)){
header("Location:error_page.php");
exit;
}
if (empty($email)){
header("Location:error_page.php");
exit;
}
if (empty($password)){
header("Location:error_page.php");
exit;
}
// Search if the user already exist in the database
$mysql = "SELECT * FROM userdata WHERE email = '".$email."'";
$result = mysql_query($mysql);
// If the query failed display error
if(!$result){
header("Location:error_page.php"); // [color=#00FF40]<--- Here is the error![/color]
exit;
}
if(($row = mysql_fetch_assoc($result)) == True) {
// User already exist
header("Location:error_page.php");
exit;
}else{
// ENcrypt password
$password = md5($password);
// Generate Activation Number
$random = (rand()%1000000);
$mysql = "INSERT INTO `userdata` (name, email, activationpassword, password, validation, banned)
VALUES ('$name', '$email', '$email', $random, $password, 'no', 'no')";
// Execute mysql query
$result = mysql_query($mysql);
// If the query failed, display error
if(!$result){
header("Location:error_page.php");
exit;
}else{
// Send the activation email to the user
$to = $email;
$subject = "activation email";
$body = "Hi there!";
if (mail($to, $subject, $body)) {
header("Location:thanks.php");
} else {
header("Location:error_page.php");
}
}
}
mysql_close($con);
}else{
header("Location:error_page.php");
exit;
}
?>
function doesExist($data, $table, $field) {
//checks to see if the data provided exists in the database
//returns 0 if false, 1 if true
$query = "SELECT COUNT(*) FROM " . $table . " WHERE " . $field . " = '" . $data . "'";
$result = mysql_query($query);
if (mysql_result($result, 0) > 0) {
$doesExist = 1;
} else {
$doesExist = 0;
}
return $doesExist;
}
When you want something to happen if the user can't be found, just use if($doesExist == 0)
Or if($doesExist == 1) if it's something to be executed if the user does exist.
Ok, when you say an error occurs, you mean your query didn't work? You are not saying there is a code problem right? I don't see one off hand your if statement looks right.
Check the values being passed from _request. How are these sent from your form? Normally _post or _get are used. I think that is your problem and your database code is working because your data is not what you think it is.
Hi there,
I rewrote the entire code again and could track where the error is actually occurring. It looks like my insert code is not working. Can some please guide me on this?
name varchar(30) Yes NULL
email varchar(60) No
activationpassword float Yes NULL
password text Yes NULL
validation char(20) Yes no
banned char(20) Yes no