Advertisement
haikelfazzani

How To Insert Into Database |Input : Text ,Radio And Select

Jul 25th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.57 KB | None | 0 0
  1. <?php
  2.  
  3.     // include file connection database
  4.    
  5.     require_once("connection.php");
  6.    
  7.     if($_SERVER["REQUEST_METHOD"] == "POST"){
  8.        
  9.         $userName = $_POST["username"];
  10.         $Gender = $_POST["gender"];
  11.         $Country = $_POST["country"];
  12.         $Date = date("y-m-d");
  13.        
  14.         $sql = "INSERT INTO upload(username,gender,country,date)
  15.         VALUES(:username,:gender,:country,:date)";
  16.        
  17.         $stmt = $connect->prepare($sql);
  18.        
  19.         // bind value to a paramater
  20.        
  21.         $stmt->bindValue(":username",$userName,PDO::PARAM_STR);
  22.         $stmt->bindValue(":gender",$Gender,PDO::PARAM_STR);
  23.         $stmt->bindValue(":country",$Country,PDO::PARAM_STR);
  24.         $stmt->bindValue(":date",$Date,PDO::PARAM_STR);
  25.        
  26.         $res = $stmt->execute();
  27.        
  28.         if($res){
  29.            
  30.             $msg = "new record add to database";
  31.            
  32.         }else{
  33.            
  34.             $msg = "Error";
  35.             exit();
  36.         }
  37.     }
  38. $connect = NULL;
  39. ?>
  40.  
  41.  
  42. <?php  require_once("head.html"); ?>
  43.  
  44. <div class="container">
  45.     <center><h3>CodeJs : How To Insert Data Into Database And display it <br />
  46.                 Input types = text , radio , select , submit </h3></center><br /><br />
  47.    
  48.         <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" enctype="multipart/form-data">
  49.        
  50.             <div class="form-group"> <!-- input text -->
  51.                 <label for="username">Username :</label>
  52.                 <input type="text" class="form-control" placeholder="Your Username" name="username">
  53.             </div>
  54.            
  55.             <div class="radio"> <!-- Radio Buttons -->
  56.                 Man : <label class="radio-inline"><input type="radio" name="gender" value="man"></label>
  57.                 Woman : <label class="radio-inline"><input type="radio" name="gender" value="woman"></label>
  58.             </div>
  59.            
  60.             <div class="form-group"> <!-- Select List -->
  61.                 <label for="sel1">Country :</label>
  62.                
  63.                     <select class="form-control" id="sel1" name="country">
  64.                         <option value="Tunisia">Tunisia</option>
  65.                         <option value="USA">USA</option>
  66.                         <option value="France">France</option>
  67.                         <option value="Russia">Russia</option>
  68.                     </select>
  69.                    
  70.             </div>
  71.            
  72.             <button type="submit" class="btn btn-primary">Register</button> <!-- button submit -->
  73.            
  74.             <br /><br />
  75.            
  76.  
  77.             <?php if(isset($msg)){ ?> <!-- Alert Message -->
  78.  
  79.                 <div class="alert alert-danger">
  80.  
  81.                     <?php echo $msg;  ?>
  82.  
  83.                 </div>
  84.  
  85.             <?php } ?> <!-- End Alert Message -->
  86.            
  87.         </form>
  88. </div>
  89.  
  90.  
  91. <style>
  92.  
  93. form, h2{ width: 400px; margin: 0 auto; }
  94. </style>
  95. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  96. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  97. </body>
  98. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement