Advertisement
Skorpius

Insert Users

Mar 3rd, 2023
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.39 KB | None | 0 0
  1. //Insert User from form - Prepared statements with placeholders
  2. <?php
  3. //INSERT USER
  4.     if(isset($_POST['submit'])){
  5.         $uname = trim($_POST['username']);
  6.         $email = trim($_POST['email']);
  7.         $pwd = "secret";
  8.  
  9.         if(empty($uname) || empty($email)){
  10.             $error = true;
  11.         }else{
  12.           $sql = 'INSERT INTO users SET user_name = ?, user_email = ?, user_password = ?';
  13.           $stmt = mysqli_stmt_init($link);
  14.           if(!mysqli_stmt_prepare($stmt, $sql)){
  15.             die('Query failed');
  16.           }else{
  17.             mysqli_stmt_bind_param($stmt, 'sss', $uname,$email,$pwd);
  18.             mysqli_stmt_execute($stmt);
  19.           }
  20.         }
  21.     }
  22. ?>
  23.  
  24. //Form
  25. <form class="py-4" action="index.php" method="post">
  26.    <div class="row">
  27.        <div class="col-md-4">
  28.           <input type="text" name="username" class="form-control" placeholder="Username" aria-label="Username">
  29.         </div>
  30.         <div class="col-md-4">
  31.           <input type="email" name="email" class="form-control" placeholder="Email Address" aria-label="Email Address">
  32.         </div>
  33.         <div class="d-grid gap-2 col-4 mx-auto">
  34.           <input type="submit" name="submit" class="form-control btn btn-secondary" value="Add New User">
  35.           <?php echo isset($error) ? "<p>Field can't be blank</p>": ''; ?>
  36.         </div>
  37.     </div>//End of row
  38. </form>
  39.  
  40.  
  41.            
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement