Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $usererr = $pwderr = $nameerr = $emailerr = "";
- $user = $pwd = $name = $email = "";
- $exec = FALSE ;
- if ($_SERVER["REQUEST_METHOD"] == "POST"){
- if (empty($_POST["username"])) {
- $usererr = "Userame is required";
- $exec = FALSE;
- }
- else {
- $user = test_input($_POST["username"]);
- if (strlen($user) < 5){
- $usererr = "Username should be between 5 to 10 characters";
- $exec = FALSE;
- }
- elseif(strlen($user) > 10){
- $usererr = "Username should be between 5 to 10 characters";
- $exec = FALSE;
- }
- }
- if (empty($_POST["password"])) {
- $pwderr = "Password is required";
- $exec = FALSE;
- }
- else {
- $pwd = test_input($_POST["password"]);
- if(!preg_match('/^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{5,}$/', $pwd)){
- $pwderr="Password should be at least 5 characters long with one letter, digit and special character";
- $exec = FALSE;
- }
- $pwd = sha1($pwd);
- }
- if (empty($_POST["name"])) {
- $nameerr = "Full name is required";
- $exec = FALSE;
- }
- else {
- $name = test_input($_POST["name"]);
- }
- if (empty($_POST["email"])) {
- $emailerr = "Email is required";
- $exec = FALSE;
- }
- else {
- $email = test_input($_POST["email"]);
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $emailerr = "Invalid email format";
- $exec = FALSE;
- }
- }
- if($exec)
- {
- $SERVER = 'localhost:9000';
- $USERNAME = 'root';
- $PASSWORD = 'password';
- $DATABASE = 'Task3';
- //$PORT = 9000
- $conn = new mysqli($SERVER,$USERNAME,$PASSWORD,$DATABASE);
- if( $conn->connect_error ) {
- die('Could not connect: ' . $conn->connect_error);
- }
- $sql = "INSERT INTO Table1 (username, password, fullname,
- email) VALUES ('$user', '$pwd', '$name', '$email')";
- /*if ($stm = $conn->prepare("INSERT INTO Table1 (username, password, fullname, email) VALUES ( ?, ?, ?, ?)"))
- {
- $stm->bind_param("sssssss", $user, $pwd, $name, $email); //preventing sql injection
- if($stm->execute() == TRUE)
- {
- echo " New record created successfully " ;
- }
- else
- {
- echo "Error: " . $conn->error;
- }
- stm->close(); }*/
- $conn->close();
- }
- }
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- ?>
- <htmllang="en-IN">
- <head>
- <title>Task3</title>
- <style>
- .error {color: #FF0000;}
- </style>
- </head>
- <body>
- <h1>Test Form</h1>
- <p><span class="error">* required </span></p>
- <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- Username: <input type="text" name="username" value="<?php echo $user;?>"/>
- <span class="error">* <?php echo $usererr;?></span><br><br>
- Password: <input type="text" name="password"value="<?php echo $pwd;?>" />
- <span class="error">* <?php echo $pwderr;?></span><br><br>
- Full Name: <input type="text" name="name"value="<?php echo $name;?>" />
- <span class="error">* <?php echo $nameerr;?></span><br><br>
- E-mail address: <input type="text" name="email" value="<?php echo $email;?>"/>
- <span class="error">* <?php echo $emailerr;?></span><br><br>
- <input type="submit" value="Submit">
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement