Postingan lainnya
HELP! Kenapa hasil parsing null atau [ ] ?
<a href='https://ibb.co/ySgmq9D'>Postman result</a><?php
//DB connection using variable-> $con
require_once 'config.inc.php';
$response = array();
if(isset($_REQUEST['apicall'])){
switch($_REQUEST['apicall']){
case 'login':
if(isTheseParametersAvailable(array('userID', 'password'))){
//$id = $_POST['idUser']
$username = $_REQUEST['userID'];
$password = $_REQUEST['password'];
$stmt = $con->prepare("SELECT userID, password FROM tbl_userprospek WHERE userID = ? AND password = ?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0){
$stmt->bind_result($username, $password);
$stmt->fetch();
$user = array(
'userID'=>$username,
'password'=>$password,
);
$response['error'] = false;
$response['message'] = 'success';
$response['user'] = $user;
}else{
$response['error'] = false;
$response['message'] = 'Invalid username or password';
}
}
break;
case 'register':
if(isTheseParametersAvailable(array('userID','email','password'))){
$username = isset($_POST['userID']);
$email = isset($_POST['email']);
$password = isset($_POST['password']);
//$gender = $_POST['gender'];
$stmt = $con->prepare("SELECT idUser FROM tbl_userprospek WHERE userID = ? OR email = ? ");
$stmt->bind_param("ss", $username, $email);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0){
$response['error'] = true;
$response['message'] = 'User already registered';
$stmt->close();
}else{
//if user is new creating an insert query
$stmt = $con->prepare("INSERT INTO tbl_userprospek (userID, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $username, $email, $password);
//if the user is successfully added to the database
if($stmt->execute()){
//fetching the user back
$stmt = $con->prepare("SELECT idUser, userID, password, email FROM tbl_userprospek WHERE userID = ?");
$stmt->bind_param("s",$username);
$stmt->execute();
$stmt->bind_result($username, $email, $password);
$stmt->fetch();
$user = array(
'idUser'=>$id,
'userID'=>$username,
'email'=>$email,
'password'=>$password
);
$stmt->close();
//adding the user data in response
$response['error'] = false;
$response['message'] = 'User registered successfully';
$response['user'] = $user;
}
}
}else{
$response['error'] = true;
$response['message'] = 'required parameters are not available';
}
break;
default: //default result
$response['error'] = true;
$response['message'] = 'Invalid Operation Called';
}//end case
//if params
}else{
//if it is not api call
//pushing appropriate values to response array
$response['error'] = true;
$response['message'] = 'Invalid API Call';
}
echo json_encode($response);
//function validating all the paramters are available
//we will pass the required parameters to this function
function isTheseParametersAvailable($params){
//traversing through all the parameters
foreach($params as $param){
///if the paramter is not available
if(!isset($_REQUEST[$param])){
return false;
}
}
//return true if every param is available
return true;
}
0
Tanggapan
halo ikut aturan main forum ya, judul pertanyaan tidak usah menggunakan "help" dan sebagainya cukup eprtanyannya saja