Guest User

Untitled

a guest
Feb 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. <?php
  2.  
  3. $json = file_get_contents('php://input');
  4. $obj = json_decode($data,true);
  5.  
  6. //Database Connection
  7. require_once 'db.php';
  8.  
  9. /* insert data into DB */
  10. foreach($obj as $item) {
  11. mysql_query("INSERT INTO `database name`.`table name` (name, phone, city, email)
  12. VALUES ('".$item['name']."', '".$item['phone']."', '".$item['city']."', '".$item['email']."')");
  13.  
  14. }
  15. //database connection close
  16. mysql_close($con);
  17.  
  18. //}
  19. ?>
  20.  
  21. <?php
  22.  
  23. //ENTER YOUR DATABASE CONNECTION INFO BELOW:
  24. $hostname="localhost";
  25. $database="dbname";
  26. $username="username";
  27. $password="password";
  28.  
  29. //DO NOT EDIT BELOW THIS LINE
  30. $link = mysql_connect($hostname, $username, $password);
  31. mysql_select_db($database) or die('Could not select database');
  32. ?>
  33.  
  34. $json = file_get_contents('php://input');
  35. $obj = json_decode($json,true);
  36.  
  37. $obj = json_decode($json,true);
  38.  
  39. <?php
  40.  
  41. require_once('dbconnect.php');
  42.  
  43. // reading json file
  44. $json = file_get_contents('userdata.json');
  45.  
  46. //converting json object to php associative array
  47. $data = json_decode($json, true);
  48.  
  49. // processing the array of objects
  50. foreach ($data as $user) {
  51. $firstname = $user['firstname'];
  52. $lastname = $user['lastname'];
  53. $gender = $user['firstname'];
  54. $username = $user['username'];
  55.  
  56. // preparing statement for insert query
  57. $st = mysqli_prepare($connection, 'INSERT INTO users(firstname, lastname, gender, username) VALUES (?, ?, ?, ?)');
  58.  
  59. // bind variables to insert query params
  60. mysqli_stmt_bind_param($st, 'ssss', $firstname, $lastname, $gender, $username);
  61.  
  62. // executing insert query
  63. mysqli_stmt_execute($st);
  64. }
  65.  
  66. ?>
  67.  
  68. $string=mysql_real_escape_string($json);
Add Comment
Please, Sign In to add comment