Ellie29

Untitled

Dec 8th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.61 KB | None | 0 0
  1. <?php
  2. /*
  3.  * Following code will create a new user row
  4.  * All product details are read from HTTP Post Request
  5.  */
  6.  
  7. // array for JSON response
  8. $response = array();
  9.  
  10. //check for required fields
  11. if (isset($_POST['title']) && isset($_POST['amount']) && isset($_POST['currencyID']) && isset($_POST['date']) && isset($_POST['groupID']) && isset($_POST['expensesTableID'])){
  12.  
  13.     $title = $_POST['title'];
  14.     $amount = $_POST['amount'];
  15.     $currencyID = $_POST['currencyID'];
  16.     $date = $_POST['date'];
  17.     $groupID = $_POST['groupID'];
  18.     $expensesTableID = $_POST['expensesTableID'];
  19.  
  20.     //include db configuration class
  21.     require_once __DIR__ . '/db_config.php';
  22.  
  23.     //connecting to database
  24.     $mysqli = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE) or die(mysql_error());
  25.    
  26.     if ($mysqli->connect_errno) {
  27.         printf("Connect failed: %s\n", $mysqli->connect_error);
  28.         exit();
  29.     }
  30.  
  31.     // mysql inserting a new row
  32.     $result = mysqli_query($mysqli,"INSERT INTO transaction(title, amount, currencyID, date, groupID, expensesTableID) VALUES('$title', '$amount', '$currencyID', '$date', '$groupID', '$expensesTableID');");
  33.    
  34.     // check if row inserted or not
  35.      if ($result) {
  36.              // successfully inserted into database
  37.             $response["success"] = 1;
  38.             $response["message"] = "Transaction successfully created.";
  39.  
  40.             // echoing JSON response
  41.             echo json_encode($response);
  42.     }
  43. }else{
  44.  
  45.     // required field is missing
  46.         $response["success"] = 0;
  47.         $response["message"] = "Transaction field(s) is missing";
  48.  
  49.         // echoing JSON response
  50.         echo json_encode($response);
  51. }
  52. ?>
Advertisement
Add Comment
Please, Sign In to add comment