Advertisement
irfanamir

create.php

Nov 12th, 2020
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.49 KB | None | 0 0
  1. <?php
  2. // Include config file
  3. require_once "config.php";
  4.  
  5. // Define variables and initialize with empty values
  6. $name = $address = $salary = "";
  7. $name_err = $address_err = $salary_err = "";
  8.  
  9. // Processing form data when form is submitted
  10. if($_SERVER["REQUEST_METHOD"] == "POST"){
  11.     // Validate name
  12.     $input_name = trim($_POST["name"]);
  13.     if(empty($input_name)){
  14.         $name_err = "Please enter a name.";
  15.     } elseif(!filter_var($input_name, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
  16.         $name_err = "Please enter a valid name.";
  17.     } else{
  18.         $name = $input_name;
  19.     }
  20.  
  21.     // Validate address
  22.     $input_address = trim($_POST["address"]);
  23.     if(empty($input_address)){
  24.         $address_err = "Please enter an address.";
  25.     } else{
  26.         $address = $input_address;
  27.     }
  28.  
  29.     // Validate salary
  30.     $input_salary = trim($_POST["salary"]);
  31.     if(empty($input_salary)){
  32.         $salary_err = "Please enter the salary amount.";
  33.     } elseif(!ctype_digit($input_salary)){
  34.         $salary_err = "Please enter a positive integer value.";
  35.     } else{
  36.         $salary = $input_salary;
  37.     }
  38.  
  39.     // Check input errors before inserting in database
  40.     if(empty($name_err) && empty($address_err) && empty($salary_err)){
  41.         // Prepare an insert statement
  42.         $sql = "INSERT INTO employees (name, address, salary) VALUES (?, ?, ?)";
  43.  
  44.         if($stmt = mysqli_prepare($link, $sql)){
  45.             // Bind variables to the prepared statement as parameters
  46.             mysqli_stmt_bind_param($stmt, "sss", $param_name, $param_address, $param_salary);
  47.  
  48.             // Set parameters
  49.             $param_name = $name;
  50.             $param_address = $address;
  51.             $param_salary = $salary;
  52.  
  53.             // Attempt to execute the prepared statement
  54.             if(mysqli_stmt_execute($stmt)){
  55.                 // Records created successfully. Redirect to landing page
  56.                 header("location: index.php");
  57.                 exit();
  58.             } else{
  59.                 echo "Something went wrong. Please try again later.";
  60.             }
  61.         }
  62.  
  63.         // Close statement
  64.         mysqli_stmt_close($stmt);
  65.     }
  66.  
  67.     // Close connection
  68.     mysqli_close($link);
  69. }
  70. ?>
  71.  
  72. <!DOCTYPE html>
  73. <html lang="en">
  74. <head>
  75.     <meta charset="UTF-8">
  76.     <title>Create Record</title>
  77.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  78.     <style type="text/css">
  79.         .wrapper{
  80.             width: 500px;
  81.             margin: 0 auto;
  82.         }
  83.     </style>
  84. </head>
  85. <body>
  86.     <div class="wrapper">
  87.         <div class="container-fluid">
  88.             <div class="row">
  89.                 <div class="col-md-12">
  90.                     <div class="page-header">
  91.                         <h2>Tambah Record</h2>
  92.                     </div>
  93.                     <p>Silahkan isi form di bawah ini kemudian submit untuk menambahkan data pegawai ke dalam database.</p>
  94.                     <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  95.                         <div class="form-group <?php echo (!empty($name_err)) ? 'has-error' : ''; ?>">
  96.                             <label>Nama</label>
  97.                             <input type="text" name="name" class="form-control" value="<?php echo $name; ?>">
  98.                             <span class="help-block"><?php echo $name_err;?></span>
  99.                         </div>
  100.                         <div class="form-group <?php echo (!empty($address_err)) ? 'has-error' : ''; ?>">
  101.                             <label>Alamat</label>
  102.                             <textarea name="address" class="form-control"><?php echo $address; ?></textarea>
  103.                             <span class="help-block"><?php echo $address_err;?></span>
  104.                         </div>
  105.                         <div class="form-group <?php echo (!empty($salary_err)) ? 'has-error' : ''; ?>">
  106.                             <label>Salary</label>
  107.                             <input type="text" name="salary" class="form-control" value="<?php echo $salary; ?>">
  108.                             <span class="help-block"><?php echo $salary_err;?></span>
  109.                         </div>
  110.                         <input type="submit" class="btn btn-primary" value="Submit">
  111.                         <a href="index.php" class="btn btn-default">Cancel</a>
  112.                     </form>
  113.                 </div>
  114.             </div>
  115.         </div>
  116.     </div>
  117. </body>
  118. </html>
  119.  
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement