Guest User

ceaphpevent

a guest
Jan 21st, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1. <html>
  2.  
  3. <head><title>insert form</title></head>
  4.  
  5. <body bgcolor="1e9dae" text="white">
  6.  
  7. <font face="roboto" >
  8. <p> FORM INSERTION</p><br>
  9. <form action="add.php" method="post">
  10. <table>
  11.  <tr><td>name :<td><input type="text" name="name"><td><tr>
  12.  <br>
  13.  <tr><td>email :<td><input type="text" name="email"><td><tr>
  14.  <br>
  15.   <tr><td>age :<td><input type="int" name="age"><td><tr>
  16.  <br>
  17.  <tr><td>adress :<td><textarea rows="4" cols="20" name="address"></textarea><td><tr>
  18.  <br>
  19.  
  20. </table>
  21. gender :<br>
  22.   <input type="radio" name="gender" value="1" checked> Male<br>
  23.   <input type="radio" name="gender" value="2"> Female<br>
  24.   <input type="radio" name="gender" value="3"> Other <br>
  25.  
  26. marital status :
  27.  
  28.  <select name="marstatus" id="marstatus">
  29.    <option value=""> -select- </option>
  30.    <option value="1">married</option>
  31.    <option value="2">unmarried</option>
  32.  </select>
  33.  <br>
  34.  <input type="Submit" name="Submit">Submit</input>
  35.  
  36. </form>
  37. </font>
  38.  
  39. </body>
  40.  
  41. </html>
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. <html>
  60. <head>
  61.     <title>Add Data</title>
  62. </head>
  63.  
  64. <body>
  65. <?php
  66. //including the database connection file
  67. require_once("config.php");
  68.  
  69. if(isset($_POST['Submit'])) {  
  70.     $name = mysqli_real_escape_string($mysqli, $_POST['name']);
  71.     $age = mysqli_real_escape_string($mysqli, $_POST['age']);
  72.     $email = mysqli_real_escape_string($mysqli, $_POST['email']);
  73.         $address = mysqli_real_escape_string($mysqli, $_POST['address']);
  74.         if(isset($_POST['gender']) && !empty($_POST['gender'])){
  75.             $gender = mysqli_real_escape_string($mysqli, $_POST['gender']);
  76.         }else{
  77.             $gender = '';
  78.         }
  79.         $marital_status = mysqli_real_escape_string($mysqli, $_POST['marstatus']);
  80.        
  81.     // checking empty fields
  82.     if(empty($name) || empty($age) || empty($email) || empty($address) || empty($gender) || empty($marital_status)) {
  83.                
  84.         if(empty($name)) {
  85.             echo "<font color='red'>Name field is empty.</font><br/>";
  86.         }
  87.        
  88.         if(empty($age)) {
  89.             echo "<font color='red'>Age field is empty.</font><br/>";
  90.         }
  91.        
  92.         if(empty($email)) {
  93.             echo "<font color='red'>Email field is empty.</font><br/>";
  94.         }
  95.                 if(empty($address)) {
  96.             echo "<font color='red'>Address field is empty.</font><br/>";
  97.         }
  98.                 if(empty($gender)) {
  99.             echo "<font color='red'>Gender field is empty.</font><br/>";
  100.         }
  101.                 if(empty($marital_status)) {
  102.             echo "<font color='red'>Marital Status field is empty.</font><br/>";
  103.         }
  104.        
  105.         //link to the previous page
  106.         echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
  107.     } else {
  108.         // if all the fields are filled (not empty)
  109.             $date = date('Y-m-d H:i:s');
  110.         $query = "INSERT INTO USERS1(NAME,AGE,EMAIL,ADRESS,GENDER,MARITAL_STATUS,DATE) VALUES('$name','$age','$email','$address','$gender','$marital_status','$date');";
  111.         //insert data to database  
  112.         $result = mysqli_query($mysqli,$query);
  113.        
  114.         //display success message
  115.         echo "<font color='green'>Data added successfully.";
  116.         echo "<br/><a href='index.php'>View Result</a>";
  117.     }
  118. }
  119. ?>
  120. </body>
  121. </html>
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. <?php
  132. /*
  133. // mysql_connect("database-host", "username", "password")
  134. $conn = mysql_connect("localhost","root","root")
  135.             or die("cannot connected");
  136.  
  137. // mysql_select_db("database-name", "connection-link-identifier")
  138. @mysql_select_db("test",$conn);
  139. */
  140.  
  141. /**
  142.  * mysql_connect is deprecated
  143.  * using mysqli_connect instead
  144.  */
  145.  
  146. $databaseHost = 'localhost';
  147. $databaseName = 'TESTDB';
  148. $databaseUsername = 'root';
  149. $databasePassword = 'LAB2';
  150.  
  151. $mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
  152. if (mysqli_connect_errno()) {
  153.     die("Connection failed: " . mysqli_connect_errno());
  154. }
  155.  
  156. ?>
Add Comment
Please, Sign In to add comment