Created
April 1, 2016 19:06
-
-
Save rushdimohamed09/107a402a09dfe92d2c5b0d3c10daa5ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once("functions.php"); | |
require_once("db-const.php"); | |
// define variables and set to empty values | |
$unameErr = $fnameErr = $lnameErr = $emailErr = $cemailErr = $passwordErr = $cpasswordErr = $cnumberErr = $addressErr = ""; | |
$uname = $fname = $lname = $email = $cemail = $password1 = $cpassword1 = $cnumber = $address = ""; | |
if ($_SERVER["REQUEST_METHOD"] == "POST") { | |
if (empty($_POST["uname"])) { | |
$unameErr = "User name is required"; | |
} else { | |
$uname = test_input($_POST["uname"]); | |
if (!preg_match("/^[0-9a-zA-Z\s-]*$/",$uname)) { | |
$unameErr = "Only letters and numbers allowed"; | |
} | |
} | |
if (empty($_POST["cnumber"])) { | |
$cnumberErr = "Contact number is required"; | |
} else { | |
$cnumber = test_input($_POST["cnumber"]); | |
if (!preg_match("/^[0-9]*$/",$cnumber)) { | |
$unameErr = "Only 0-9 numbers are allowed"; | |
} | |
} | |
if (empty($_POST["fname"])) { | |
$fnameErr = "First name is required"; | |
} else { | |
$fname = test_input($_POST["fname"]); | |
// check if name only contains letters and whitespace | |
if (!preg_match("/^[a-zA-Z ]*$/",$fname)) { | |
$fnameErr = "Only letters and white space allowed"; | |
} | |
} | |
if (empty($_POST["lname"])) { | |
$lnameErr = "Last name is required"; | |
} else { | |
$lname = test_input($_POST["lname"]); | |
// check if name only contains letters and whitespace | |
if (!preg_match("/^[a-zA-Z ]*$/",$lname)) { | |
$lnameErr = "Only letters and white space allowed"; | |
} | |
} | |
if (empty($_POST["email"])) { | |
$emailErr = "Email is required"; | |
} else { | |
$email = test_input($_POST["email"]); | |
// check if e-mail address is well-formed | |
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
$emailErr = "Invalid email format"; | |
} | |
} | |
if (empty($_POST["cemail"])) { | |
$cemailErr = "Confirm email is required"; | |
} else { | |
$cemail = test_input($_POST["cemail"]); | |
// check if e-mail address is well-formed | |
if (!filter_var($cemail, FILTER_VALIDATE_EMAIL)) { | |
$cemailErr = "Invalid email format"; | |
} | |
} | |
if (empty($_POST["password1"])) { | |
$passwordErr = "Password is required"; | |
} | |
if (empty($_POST["cpassword1"])) { | |
$cpasswordErr = "Confirm password is required"; | |
} | |
if (empty($_POST["address"])) { | |
$addressErr = "Please enter your address"; | |
} | |
} | |
function test_input($data) { | |
$data = trim($data); | |
$data = stripslashes($data); | |
$data = htmlspecialchars($data); | |
return $data; | |
} | |
if (isset($_POST['submit'])) { | |
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); | |
# check connection | |
if ($mysqli->connect_errno) { | |
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>"; | |
} | |
$sql = "INSERT INTO leebay (id, fname, lname, email, username, password, address)VALUES ('".NULL."','".$fname."','".$lname."','".$email."','".$uname."','".$password1."','".$address."',)"; | |
if (mysqli_query($mysqli, $sql)) { | |
echo "New record created successfully"; | |
} else { | |
echo "Error: " . $sql . "<br>" . mysqli_error($conn); | |
} | |
mysqli_close($mysqli); | |
} | |
?> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>Untitled Document</title> | |
<style> | |
.error {color: #FF0000;} | |
</style> | |
</head> | |
<body> | |
<form action="<?=$_SERVER['PHP_SELF']?>" method="post"> | |
<fieldset> | |
<h2>PERSONAL INFORMATION</h2> | |
<span class="error">* required field.</span></p> | |
<hr class="colorgraph"> | |
<div class="form-group"> | |
<input name="uname" type="text" value="<?php echo $uname;?>" class="form-control input-lg" placeholder="Username"> | |
<span class="error">* <?php echo $unameErr;?></span> | |
</div> | |
<div class="form-group"> | |
<input name="fname" type="text" value="<?php echo $fname;?>" class="form-control input-lg" placeholder="First Name"> | |
<span class="error">* <?php echo $fnameErr;?></span> | |
</div> | |
<div class="form-group"> | |
<input name="lname" type="text" value="<?php echo $lname;?>" class="form-control input-lg" placeholder="Last Name"> | |
<span class="error">* <?php echo $lnameErr;?></span> | |
</div> | |
<div class="form-group"> | |
<input name="email" type="text" value="<?php echo $email;?>" class="form-control input-lg" placeholder="Email Address"> | |
<span class="error">* <?php echo $emailErr;?></span> | |
</div> | |
<div class="form-group"> | |
<input name="cemail" type="text" value="<?php echo $cemail;?>" class="form-control input-lg" placeholder="Confirm Email Address"> | |
<span class="error">* <?php echo $cemailErr;?></span> | |
</div> | |
<div class="form-group"> | |
<input name="password" type="password" value="<?php echo $password;?>" class="form-control input-lg" placeholder="Password"> | |
<span class="error">* <?php echo $passwordErr;?></span> | |
</div> | |
<div class="form-group"> | |
<input name="cpassword" type="password" value="<?php echo $cpassword;?>" class="form-control input-lg" placeholder="Confirm Password"> | |
<span class="error">* <?php echo $cpasswordErr;?></span> | |
</div> | |
<hr class="colorgraph"> | |
<div class="row"> | |
<div class="col-xs-6 col-sm-6 col-md-6"> | |
<input type="submit" name="Submit" value="Register" class="btn btn-lg btn-success btn-block"> | |
</div> | |
<div class="col-xs-6 col-sm-6 col-md-6"> | |
<input type="reset" value="Reset form" class="btn btn-lg btn-success btn-block"> | |
</div> | |
</div> | |
</fieldset> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please separate your view and logic . For better maintainability