Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Insert User from form - Prepared statements with placeholders
- <?php
- //INSERT USER
- if(isset($_POST['submit'])){
- $uname = trim($_POST['username']);
- $email = trim($_POST['email']);
- $pwd = "secret";
- if(empty($uname) || empty($email)){
- $error = true;
- }else{
- $sql = 'INSERT INTO users SET user_name = ?, user_email = ?, user_password = ?';
- $stmt = mysqli_stmt_init($link);
- if(!mysqli_stmt_prepare($stmt, $sql)){
- die('Query failed');
- }else{
- mysqli_stmt_bind_param($stmt, 'sss', $uname,$email,$pwd);
- mysqli_stmt_execute($stmt);
- }
- }
- }
- ?>
- //Form
- <form class="py-4" action="index.php" method="post">
- <div class="row">
- <div class="col-md-4">
- <input type="text" name="username" class="form-control" placeholder="Username" aria-label="Username">
- </div>
- <div class="col-md-4">
- <input type="email" name="email" class="form-control" placeholder="Email Address" aria-label="Email Address">
- </div>
- <div class="d-grid gap-2 col-4 mx-auto">
- <input type="submit" name="submit" class="form-control btn btn-secondary" value="Add New User">
- <?php echo isset($error) ? "<p>Field can't be blank</p>": ''; ?>
- </div>
- </div>//End of row
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement