0

So I have this in php and it keeps giving me an error:

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

include("JSON_Test.php");

// Takes raw data from the request
$json = file_get_contents('php://input');

// Converts it into a PHP object
$data = json_decode($json);

$integer = $data->name;

$query = "INSERT INTO Insect_Score_Board ( name , score) VALUES($integer, $data->score)";

$conn->exec($query);

echo $data->name.", ".$data->score."mommy";

?>

but if I change

$query = "INSERT INTO Insect_Score_Board ( name , score) VALUES('random ', $data->score)";

only $data->name seems to be giving me the problem

Alex Bravo
  • 1,601
  • 2
  • 24
  • 40
  • Unrelated, but your code is vulnerable to SQL injections. [See this](https://stackoverflow.com/q/60174/2302862). – Siguza Apr 26 '20 at 22:04
  • try to echo $data->name and see what it is in it, also echo the pdo error and copy the exact error message to your question. – nbk Apr 26 '20 at 22:12
  • Could you please provide a bit more details on your JSON file. – Oleg Andreyev Apr 26 '20 at 22:17

1 Answers1

0

if $data->name is a string, you are getting the error, if it is an actual integer it will succeed.

Either provide the structure of your table, or try:

$query = "INSERT INTO Insect_Score_Board ( name , score) VALUES('$integer', $data->score)";

Ron
  • 5,900
  • 2
  • 20
  • 30