Reputation: 563
<?php
$value = json_decode(file_get_contents('php://input'));
$mysql_pekare= new mysqli ("serv", "user","pass", "db");
if(!empty($value)) {
$stmt = $mysql_pekare->prepare("INSERT INTO users (`username`, `password`) VALUES(?,?)");
$stmt->bind_param("ss", $value->username, $value->password);
$stmt->execute();
if(!empty($stmt)) {
$contacts = array();
$id = mysqli_insert_id();
$contact = array("objectId" => ($id));
array_push($contacts, $contact);
echo json_encode(array('results' => $contacts), JSON_PRETTY_PRINT);
}
$stmt->close();
$mysql_pekare->close();
}
?>
Now when I insert my info from my frontend the value (username + password) gets added into MySQL just fine with a unique ID but I do not get the returned ID, currently I get it like this:
{
"results": [
{
"objectId": null
}
]
}
Upvotes: 2
Views: 53
Reputation: 746
You forgot to add the database as parameter, mysqli_insert_id($link); But, anyway you mix up class style and procedural style.
Use $id = $mysql_pekare->insert_id;
Upvotes: 0
Reputation:
Based on an example in php.net this is what you want to change in your code
$id = $stmt->insert_id();
Upvotes: 2
Reputation: 2857
Your $id is most likely null or an error not sure...
//This is for procedural and needs a link
$id = mysqli_insert_id();
//should be
$id = mysqli_insert_id($mysql_pekare);
//$mysql_pekare I am assuming is your connection...
//however you are using oo (or I think) so should be
$stmt->insert_id
Upvotes: 2