I have created a registration page using PHP. The control always goes into else part('Error'). I have defined the database, used the same id used in the form fields but still its the same. I'm unable to figure this out. I'd really appreciate any help.
Here's my code :
<?php
require_once 'connectdb.php';
$dbc = mysqli_connect($mysql_host, $mysql_user , $mysql_pass , $mysql_db ) or die('Error Connecting to database');
if ( isset($_POST['submit'] ) )
{
$first_name = mysqli_real_escape_string($dbc, trim($_POST['first_name']));
$last_name = mysqli_real_escape_string($dbc, trim($_POST['last_name']));
$password1 = mysqli_real_escape_string($dbc, trim($_POST['password1']));
$password2 = mysqli_real_escape_string($dbc, trim($_POST['password2']));
$email = mysqli_real_escape_string($dbc, trim($_POST['email']));
if (!empty($first_name) && !empty($last_name) && !empty($password1) && !empty($password2) && ($password1==$password2) )
{
$query = "SELECT * FROM user_db WHERE email='$email'";
$data = mysqli_query($dbc, $query);
if(mysqli_num_rows($data) == 0)
{
$password2 = md5($password1);
$query = "INSERT INTO user_db(fname, lname, email, password) VALUES('$first_name','$last_name','$email','$password2')";
$data = mysqli_query($dbc, $query) or die('Error Querying database-Adding info');
echo '<strong>Registration Successful</strong>';
mysqli_close($dbc);
exit();
}
else
{
echo 'An account already exists for with this email id. Kindly use a different one<br /> ';
$email = "";
}
}
else
{
echo 'Please enter all the fields correctly, including the desired password correctly <br/>';
}
}
// The control goes everytime into this else part
else
{
echo 'Error';
}
mysqli_close($dbc);
?>
$_POST['submit']
is not set. This is why your code goes into the "error"-part.
You must set names of the html-elements so PHP knows what to look for.
Something like this:
<form action="index.php" method="POST" >
First Name : <input type="text" id="first_name" name="first_name" /><br/>
Last Name : <input type="text" id="last_name" name="last_name" /><br/>
Email : <input type="text" id="email" name="email" /><br/>
Password : <input type="password" id="password1" name="password1" /><br/>
Repeat Password:<input type="password" id="password2" name="password2" /><br/>
<input type="submit" id="submit" value="submit" name="submit" /> </form>
Also consider not to mix types of html-elements and id/names of those. submit is not the ultimate name for an html-element because it could bring a lot of confusion. Instead name the submit-button to something like:
<input type="submit" id="submit-registration" value="submit" name="submit-registration" /> </form>