Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. <?php
  2. $servername = "localhost:9000";
  3. $username = "root";
  4. $password = "31Godavari!";
  5.  
  6. // Create connection
  7. $conn = new mysqli($servername, $username, $password, $dbname);
  8. // Check connection
  9. if (!$conn) {
  10. die("Connection failed: " . $conn->error);
  11. }
  12.  
  13. // Create database
  14. $sql = "CREATE DATABASE DB";
  15. if (!$conn->query($sql)) {
  16. echo "Error creating database: " . $conn->error;
  17. }
  18. else {
  19. echo "Database created successfully";
  20. }
  21. $dbname = "DB";
  22.  
  23. // sql to create table
  24. $table = "CREATE TABLE $dbname.Details (
  25. username VARCHAR(20) PRIMARY KEY,
  26. password VARCHAR(20) NOT NULL,
  27. fullname VARCHAR(30) NOT NULL,
  28. email VARCHAR(20) NOT NULL)";
  29.  
  30. if (!$conn->query($table)) {
  31. echo "Error creating table: " . $conn->error;
  32. } else {
  33. echo "Table Details created successfully";
  34. }
  35.  
  36. $ins = "INSERT INTO Details (username, password, fullname, email)
  37. VALUES ($user, sha1($pwd), $fullname, $email)";
  38.  
  39. if ($conn->query($ins) === TRUE) {
  40. echo "New record created successfully";
  41. } else {
  42. echo "Error: " . $ins . "<br>" . $conn->error;
  43. }
  44.  
  45. $conn->close();
  46. ?>
  47.  
  48. <html lang="en-IN">
  49. <head>
  50. <title>Task3</title>
  51. <style>
  52. .error {color: #FF0000;}
  53. </style>
  54.  
  55. </head>
  56. <body>
  57. <?php
  58.  
  59. $userErr = $emailErr = $pwdErr = $fullnameErr = "";
  60. $user = $email = $pwd = $fullname = "";
  61.  
  62. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  63. if (empty($_POST["username"])) {
  64. $userErr = "UserName is required";
  65. } else {
  66. $user = test_input($_POST["username"]);
  67. if (strlen($user) < 5){
  68. $userErr = "Username should be between 5 to 10 characters";
  69. }
  70. elseif(strlen($input) > 10){
  71. $userErr = "Username should be between 5 to 10 characters";
  72. }
  73. }
  74.  
  75. if (empty($_POST["email"])) {
  76. $emailErr = "Email is required";
  77. } else {
  78. $email = test_input($_POST["email"]);
  79. // check if e-mail address is well-formed
  80. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  81. $emailErr = "Invalid email format";
  82. }
  83. }
  84.  
  85. if (empty($_POST["password"])) {
  86. $pwd = "";
  87. } else {
  88. $pwd = test_input($_POST["password"]);
  89. if(!preg_match("^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{5,}$")){
  90. $pwdErr="Password should be at least 5 characters long with one letter, digit and special character";
  91. }
  92. }
  93.  
  94. if (empty($_POST["name"])) {
  95. $fullnameErr = "Full Name is required";
  96. } else {
  97. $fullname = test_input($_POST["name"]);
  98. }
  99. }
  100.  
  101. function test_input($data) {
  102. $data = trim($data);
  103. $data = stripslashes($data);
  104. $data = htmlspecialchars($data);
  105. return $data;
  106. }
  107. ?>
  108.  
  109. <h1>Test Form</h1>
  110. <p><span class="error">* required </span></p>
  111. <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  112. Username: <input type="text" name="username" value="<?php echo $user;?>"/>
  113. <span class="error">* <?php echo $userErr;?></span><br><br>
  114. Password: <input type="text" name="password"value="<?php echo $pwd;?>" />
  115. <span class="error">* <?php echo $pwdErr;?></span><br><br>
  116. Full Name: <input type="text" name="name"value="<?php echo $fullname;?>" />
  117. <span class="error">* <?php echo $fullnameErr;?></span><br><br>
  118. E-mail address: <input type="text" name="email" value="<?php echo $email;?>"/>
  119. <span class="error">* <?php echo $emailErr;?></span><br><br>
  120. <input type="submit" value="Submit">
  121. </form>
  122. </body>
  123. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement