Ellie29

Untitled

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