-1

I've been having a bit of trouble with my PHP code. I'm trying to insert a new row in table gebruikers. I'm using a JSON API to post the data from my C# Android app to the server. running the code returns an invalid request error.

PHP:

function registerUser($api_data)
{

// connection
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$database = "test";

$mysqli = new mysqli($servername, $username, $password, $database);

//check connection
if(mysqli_connect_errno()) 
{
    API_Response(true, 'connection error');
}

$register_data = json_decode($api_data);

$leerlingnummer = intval($register_data->leerlingnummer); //passed as string, int in database
$wachtwoord = $register_data->wachtwoord;   //string
$email = $register_data->email; //string

$result = $mysqli->query("INSERT INTO `gebruikers` (`Leerlingnummer`, `Wachtwoord`, `Email`) VALUES ({$leerlingnummer}, {$wachtwoord}, {$email})");

if ($result == false)
{
    API_Response(true, "{$mysqli->error}");
}

else
{       
    API_Response(false, 'SUCCESS');
}

$mysqli->close();
}

database is looking as follows: database layout

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

0

I've never felt this stupid before, but the error came from the fact that I was still referencing to an older .php file. I was so focussed on the PHP script that I didn't notice this error in my app before.

the quotes advised by Sean and fuso were needed later on though so thanks for that.

Problem solved, sorry for wasting some of your time :/

-1

You should quote your data in the insert query.

... VALUES ('{$email}','{$other}')