SHARE
TWEET

Untitled

a guest Apr 20th, 2019 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $fields = [
  4.   'title',
  5.   'first_name',
  6.   'last_name',
  7.   'mid_name',
  8.   'gender',
  9.   'h_address',
  10.   'n_bus',
  11.   'religion',
  12.   'tel',
  13.   'occupation',
  14.   'o_address',
  15.   'dob',
  16.   'o_number',
  17.   'email',
  18.   'local',
  19.   'state',
  20.   'bank_name',
  21.   'account_name',
  22.   'account_num',
  23.   'phone',
  24.   'your_email',
  25. ];
  26.  
  27. $columns = implode(', ', $fields);
  28. $values = implode(', ', array_map(function ($field) {
  29.   return ":{$field}";
  30. }, $fields));
  31.  
  32. $data = [];
  33. foreach ($fields as $field) {
  34.   $data[$field] = isset($_POST[$field]) ? $_POST[$field] : null;
  35. }
  36. $sql = "INSERT INTO users ({$columns}) VALUES ({$values})";
  37.  
  38. try {
  39.   $dsn = "mysql:host=localhost;dbname=myDatabase;charset=utf8mb4";
  40.   $options = [
  41.     PDO::ATTR_EMULATE_PREPARES   => false,
  42.     PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
  43.     PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  44.   ];
  45.   $pdo = new PDO($dsn, "username", "password", $options);
  46.   $statement = $pdo->prepare($sql);
  47.   $statement->execute($data);
  48.   echo $pdo->lastInsertId();
  49.  
  50. } catch (Exception $e) {
  51.   error_log($e->getMessage());
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top