Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <head>
  3. <title>
  4. assignment
  5. </title>
  6. <style type="text/css">
  7. table,td{
  8. border:1px solid black;
  9. }
  10. </style>
  11.  
  12. </head>
  13.  
  14. <body>
  15. <h1 align="center">Enter Data below</h1>
  16. <br>
  17. <br>
  18. <br>
  19. <form method = "post" action="" >
  20. <table align = "center">
  21. <tr>
  22. <td><input type = "test" name = "fname" placeholder="first name"></td>
  23. </tr>
  24. <tr>
  25. <td><input type = "text" name = "lastname" placeholder="last name"></td>
  26. </tr>
  27. <tr>
  28. <td align="right"><input type = "submit" value = "submit"></td>
  29. </table>
  30. </form>
  31. </body>
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. <?php
  40.  
  41. $servername = "localhost";
  42. $username = "root";
  43. $password = "";
  44. $dbname = "1st_database";
  45. $conn = new mysqli($servername,$username,$password,$dbname);
  46. if($conn->connect_error)
  47. {
  48. die("connection failed".$conn->connect_error);
  49. }
  50. $fname = $_POST['fname'];
  51. $lname = $_POST['lastname'];
  52. if(empty($_POST['fname']) && empty($_POST['lastname']))
  53. {
  54. echo "please enter name first";
  55. }
  56. elseif($query = "SELECT First_Name FROM sample_table WHERE First_Name =
  57. '$fname'")
  58.  
  59. if(mysqli_query($conn,$query) === true)
  60. {
  61. echo "this name already exists";
  62. }
  63. else {
  64. $sql = "INSERT INTO sample_table (First_Name,Last_Name)
  65. VALUES ('$fname','$lname')";
  66. if(!$conn->query($sql) === true)
  67. {
  68. echo "Error: " . $sql . "<br>" . $conn->error;
  69. }
  70. echo "<br>";
  71. echo "<br>";
  72. echo "<br>";
  73. echo "<br>";
  74. echo "<br>";
  75. echo "<br>";
  76.  
  77. $sql = "SELECT * FROM sample_table";
  78. $result = $conn->query($sql);
  79. echo "<table>";
  80.  
  81. while($row = $result->fetch_assoc()) {
  82. echo "<tr><td><b>First Name: </b> " ."<td>".
  83. $row["First_Name"]."</td>". "</td><td><b>Last Name: </b>" ."<td>".
  84. $row["Last_Name"]."</td>". "</td></tr>";
  85. }
  86.  
  87. echo "</table>";
  88.  
  89. }
  90.  
  91.  
  92.  
  93. $conn->close();
  94. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement