This script is used with an Android app
The error : 2 Fatal error: Call to a member function bind_param() on a non-object in ** on line 36
There is three sql queries on my script the first one is running correctly but the two other one not.
I don't really understand why because I do exactly the same stuff ...
PHP
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
$con = mysqli_connect("******","******","******","******");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
// If all the input are ok
if(isset($_REQUEST['firstName']) AND isset($_REQUEST['lastName']) AND isset($_REQUEST['phone']) AND isset($_REQUEST['linkedPhone']) AND isset($_REQUEST['imei'])) {
$firstname = $_REQUEST['firstName'];
$lastName = $_REQUEST['lastName'];
$phone = $_REQUEST['phone'];
$linkedPhone = $_REQUEST['linkedPhone'];
$imei = $_REQUEST['imei'];
$req = $con->prepare('SELECT idUser FROM user WHERE phone = ?');
$req->bind_param("s", $_REQUEST['linkedPhone']);
$req->execute();
$req -> bind_result($idFollowed);
$result = $req->fetch();
$idFollowed = "".$idFollowed;
echo $idFollowed;
// if linkedPhone doesn't exist
if(!$result) {
$return['result'] = "Linked phone doesn't exist";
}
else {
$req2 = $con->prepare('INSERT INTO user (firstName, lastName, phone, idFollowed, imei) VALUES (?, ?, ?, ?, ?)');
// Work using phpMyAdmin
// INSERT INTO user (firstName, lastName, phone, idFollowed) VALUES ('Gael', 'Fontenelle', '01234', '1');
$req2->bind_param("sssss", $_REQUEST['firstName'], $_REQUEST['lastName'], $_REQUEST['phone'], $idFollowed, $_REQUEST['imei']); // ERROR HERE
$req2->execute();
// Find the id of the new user
$req3 = $con->prepare('SELECT idUser FROM user WHERE phone = ?');
$req3->bind_param("s", $_REQUEST['phone']); // ERROR HERE
$req3->execute();
$req3 -> bind_result($idUser);
$result = $req->fetch();
if(!$result) {
$return['result'] = "Error in the registration process";
}
else {
$return['result'] = "".$idUser;
}
}
}
else {
// Display the error
$return['result'] = "Missing data!";
}
echo json_encode($return);
}
mysqli_close($con);
?>
SQL
CREATE TABLE user (
idUser int NOT NULL auto_increment,
firstName varchar(50) NOT NULL,
lastName varchar(50) NOT NULL,
phone varchar(50) NOT NULL,
idFollowed int default 0, -- 0 if it's the primary user
validation boolean NOT NULL, default 0, -- 1 = VALIDATION OK
imei varchar(50) NOT NULL,
PRIMARY KEY (idUser),
FOREIGN KEY (idFollowed) REFERENCES idUser (user)
);
INSERT INTO user (firstName, lastName, phone, imei) VALUES
('Steve', 'Jobs', '0836656565651', '23456789765'),
('Steve', 'Wozniak', '0836656565652', '23456789765'),
('Bill', 'Gates', '0836656565653', '23456789765'),
('Steve', 'Balmer', '0836656565654', '23456789765'),
('Larry', 'Pagen', '0836656565655', '23456789765'),
('Serguei', 'Brin', '0836656565656', '23456789765');