0

I'm stuck here..
My api isn't receiving my post params, already try some things.
What is in the commented line (a way that i used before)..
$query->execute(['recado' => $recado, 'email' => $email, 'ajuda_tipo' => $ajuda_tipo, 'ajuda_nome' => $ajuda_nome, 'ajuda_qnt' => $ajuda_qnt, 'total' => $total]);
In the same API i have some get requests that are working like a charm.

Code:

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
header('Access-Control-Allow-Methods: GET, POST, PUT');

$db = new PDO("mysql:host=localhost;dbname=XXXXXXXXXXXXX;charset=utf8", "root", "XXXXXXXXXX");

function safeInput($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);

  return $data;
}

$json = ['status' => 'fail'];

// Post Request

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $recado = safeInput($_POST['recado']);
  $email = safeInput($_POST['email']);
  $ajuda_tipo = safeInput($_POST['ajuda_tipo']);
  $ajuda_nome = safeInput($_POST['ajuda_nome']);
  $ajuda_qnt = safeInput($_POST['ajuda_qnt']);
  $total = safeInput($_POST['total']);

  if ($email) {
    $query = $db->prepare("INSERT INTO envios_2019 (id, recado, email, ajuda_tipo, ajuda_nome, ajuda_qnt, total) VALUES (null, :recado, :email, :ajuda_tipo, :ajuda_nome, :ajuda_qnt, :total");
    $query->bindParam('recado', $recado);
    $query->bindParam('email', $email);
    $query->bindParam('ajuda_tipo', $ajuda_tipo);
    $query->bindParam('ajuda_nome', $ajuda_tipo);
    $query->bindParam('ajuda_qnt', $ajuda_qnt);
    $query->bindParam('total', $total);
    $query->execute();
    // $query->execute(['recado' => $recado, 'email' => $email, 'ajuda_tipo' => $ajuda_tipo, 'ajuda_nome' => $ajuda_nome, 'ajuda_qnt' => $ajuda_qnt, 'total' => $total]);
    if ($query->fetchColumn() >= 1) {
      $json['status'] = 'done';
      $json['envio'] = [
        'recado' => $recado,
        'email' => $email,
        'ajuda_tipo' => $ajuda_tipo,
        'ajuda_nome' => $ajuda_nome,
        'ajuda_qnt' => $ajuda_qnt,
        'total' => $total
      ];
    }
    else {
      $json['status'] = 'Erro na hora de enviar!';
    }
  }
  else {
    $json['status'] = 'fail';
  }
}

The POST request:

Post Request

POST Response: POST Response

Here's where do i call the POST REQUEST in my code, above events were using a POST Injection (Advanced REST Client for Chrome)

doEnvio: function() {
      const Axios = require('axios');
      let vm = this
      var ajuda1 = ''
      var ajuda2 = ''
      var ajuda7 = ''
      vm.ajudas.forEach(function(res){
        ajuda1 = res.tipo + ' - ' + ajuda1
        ajuda2 = res.nome + ' - ' + ajuda2
        ajuda7 = res.qnt + ' - ' + ajuda7
      })
      const params = new URLSearchParams();
      params.append('recado',  vm.envio.recado);
      params.append('email', vm.envio.email);
      params.append('ajuda_tipo', ajuda1);
      params.append('ajuda_nome', ajuda2);
      params.append('ajuda_qnt', ajuda7);
      params.append('total', vm.total);
      Axios({
        method: 'post',
        url: 'http://localhost/api/',
        data: params
      })
        .then (function(response){
          console.log(response)
        })
        .catch (function(err){
          console.log(err)
        })
    }

@edit Here's my php code now with some few changes:

// Post Request

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $_POST = json_decode(file_get_contents("php://input"), true);
  $recado = safeInput($_POST['recado']);
  $email = safeInput($_POST['email']);
  $ajuda_tipo = safeInput($_POST['ajuda_tipo']);
  $ajuda_nome = safeInput($_POST['ajuda_nome']);
  $ajuda_qnt = safeInput($_POST['ajuda_qnt']);
  $total = safeInput($_POST['total']);
  if ($email) {
    // VERIFYING
    echo "Email:" . $email . "\n";
    echo "Recado:" . $recado . "\n";
    echo "ajuda_tipo:" . $ajuda_tipo . "\n";
    echo "ajuda_nome:" . $ajuda_nome . "\n";
    echo "ajuda_qnt:" . $ajuda_qnt . "\n";
    echo "total:" . $total . "\n";
    //
    $query = $db->prepare("INSERT INTO envios_2019 (recado, email, ajuda_tipo, ajuda_nome, ajuda_qnt, total) VALUES (:recado, :email, :ajuda_tipo, :ajuda_nome, :ajuda_qnt, :total");
    $query->bindParam(':recado', $recado);
    $query->bindParam(':email', $email);
    $query->bindParam(':ajuda_tipo', $ajuda_tipo);
    $query->bindParam(':ajuda_nome', $ajuda_tipo);
    $query->bindParam(':ajuda_qnt', $ajuda_qnt);
    $query->bindParam(':total', $total);
    $query->execute();
    // $query->execute(['recado' => $recado, 'email' => $email, 'ajuda_tipo' => $ajuda_tipo, 'ajuda_nome' => $ajuda_nome, 'ajuda_qnt' => $ajuda_qnt, 'total' => $total]);
    if ($query->fetchColumn() >= 1) {
      $json['status'] = 'done';
      $json['envio'] = [
        'recado' => $recado,
        'email' => $email,
        'ajuda_tipo' => $ajuda_tipo,
        'ajuda_nome' => $ajuda_nome,
        'ajuda_qnt' => $ajuda_qnt,
        'total' => $total
      ];
    }
    else {
      $json['status'] = 'Erro na hora de enviar!';
    }
  }
  else {
    $json['status'] = 'fail';
  }
}

And here the POST RESPONSE now: (now its entering and verifying, but i think its a mysql sintax error because its returning the $query->fetchColumn() as 0 not the expected 1 enter image description here

Pablo Mariante
  • 350
  • 3
  • 11
  • Can you also post the result from the the console.log. – Bluetree Nov 05 '19 at 05:43
  • Hello! Now, after I made some changes, I added ` $_POST = json_decode(file_get_contents("php://input"), true);` this line before the if ($email) and i bind $email with `$email=$_POST['email'];` now im entering the `if ($email)` but when i try to INSERT INTO it doesnt insert. Now i can use `echo $_POST['email']` and i get with success the email from vue. My INSERT INTO sintax still the same.. – Pablo Mariante Nov 05 '19 at 05:53
  • Is your `id` set to `primary key` and auto incremented? If yes, just remove the `id` and the `null` value. – Bluetree Nov 05 '19 at 05:57
  • Yes, i removed and still not working... I'm gonna edit the post with my code now – Pablo Mariante Nov 05 '19 at 05:59
  • `$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );` add this before the `$db->prepare` Insert query. – Bluetree Nov 05 '19 at 06:08
  • Then after the `$query->execute();` add this code `print_r($db>errorInfo());` and post the message here. – Bluetree Nov 05 '19 at 06:09
  • Here's the error `Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 in C:\servidor\htdocs\api\index.php on line 46
    ` and line 46 code is that -> ` $query->execute();`
    – Pablo Mariante Nov 05 '19 at 06:10
  • 1
    You can check this https://stackoverflow.com/questions/4544051/sqlstate42000-syntax-error-or-access-violation-1064-you-have-an-error-in-you – Bluetree Nov 05 '19 at 06:12
  • With the `print_r($db->errorInfo());` it returns now this https://i.imgur.com/ewgwRlS.png – Pablo Mariante Nov 05 '19 at 06:12
  • 1
    https://stackoverflow.com/a/10556955/8892091 – Bluetree Nov 05 '19 at 06:20
  • Ok, now its `$query = $db->prepare("INSERT INTO `envios_2019` (`recado`, `email`, `ajuda_tipo`, `ajuda_nome`, `ajuda_qnt`, `total`) VALUES (:recado, :email, :ajuda_tipo, :ajuda_nome, :ajuda_qnt, :total");` but still same error from last print that one https://i.imgur.com/ewgwRlS.png – Pablo Mariante Nov 05 '19 at 06:23
  • That was a ) missing after TOTAL in my mysql query.. Now i have a new error in `PDOStatement::fetchColumn(): SQLSTATE[HY000]: General error in` this line here `if ($query->fetchColumn() >= 1)` – Pablo Mariante Nov 05 '19 at 06:27
  • 1
    Fixed! Working like a charm! My error was in the missing ) after fixed the ` ` ` in my code! Ty @Bluetree – Pablo Mariante Nov 05 '19 at 06:29
  • You're welcome. Glad you fixed it :) – Bluetree Nov 05 '19 at 06:30

0 Answers0