Guest User

Untitled

a guest
Dec 18th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. <?php
  2. //Enter your database server authentication information and name of the db you want to use
  3. $db_server = "localhost";
  4. $db_username = "username";
  5. $db_password = "password";
  6. $db_database = "dbName";
  7.  
  8. $con=mysqli_connect($db_server,$db_username,$db_password,$db_database);
  9.  
  10. $data = json_decode(file_get_contents('php://input'));
  11. if (mysqli_connect_errno())
  12. {
  13. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  14. }
  15.  
  16. // Coloumns in the table, you can chage these but they have to match up exactly with the table
  17. $columns = ["amount","receipt","transday","phonenumber"];
  18. $values = array();
  19. foreach ($data->Body->stkCallback->CallbackMetadata->Item as $item) {
  20.  
  21. if ($item->Name !== "Balance") {
  22. $values[]=$item->Value;
  23. }
  24. }
  25. $col="";
  26. $val="";
  27.  
  28. for ($i=0; $i <count($columns) ; $i++) {
  29. $col.=$columns[$i];
  30. $val.="'".$values[$i]."'";
  31.  
  32. if ($i !==(count($columns)-1)) {
  33. $col.=",";
  34. $val.=",";
  35.  
  36. }
  37. }
  38.  
  39. $transact="INSERT INTO transactions(".$col.")VALUES(".$val.")";
  40.  
  41. $exec = mysqli_query($con,$transact);
  42.  
  43. if($exec){
  44. echo "Data inserted";
  45. }else{
  46. echo "There was an error " . mysqli_error($con);
  47. }
  48.  
  49. mysqli_close($con);
  50. ?>
Add Comment
Please, Sign In to add comment