Click here to Skip to main content
15,921,990 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ERROR :
{"error":true,"message":"required parameters are not available"}


What I have tried:

<?php 

//getting db connection
require_once'db.php';

$response =array();

if (isset($_GET['apicall'])) {

    switch ($_GET['apicall']) {
        case 'signup':
            //signup validation

        if (isTheseParametersAvailable(array('do_name','do_bg','do_age','do_mobile','do_pincode','do_gender','do_city','do_state','do_mailid','do_password'))) {

            //getting the value  from database
            // $id=$_POST['id'];
            $do_name=$_POST['do_name'];
            $do_bg=$_POST['do_bg'];
            $do_age=$_POST['do_age'];
            $do_mobile=$_POST['do_mobile'];
            $do_pincode=$_POST['do_pincode'];
            $do_gender=$_POST['do_gender'];
            $do_city=$_POST['do_city'];
            $do_state=$_POST['do_state'];
            $do_mailid=$_POST['do_mailid'];
            $do_password=md5($_POST['do_password']);


//as the email and username should be unique for every user 
             $stmt = $conn->prepare("SELECT do_name FROM users WHERE do_name = ? OR do_mailid = ?");
 $stmt->bind_param("ss", $do_name, $do_mailid);
 $stmt->execute();
 $stmt->store_result();
            //user already exist

            if ($stmt->nom_rows>0 ){

            $response['error']=true;
            $response['message']='User Already Register';

            $stmt->close();
                
            }else{

                //insert query
                $stmt=$conn->prepare("INSERT INTO users(do_name,do_age,do_bg,do_mobile,do_pincode,do_gender,do_city,do_state,do_mailid,do_password) VALUE (?,?,?,?,?,?,?,?)");
                $stmt->bind_param('ssssssss',$do_name,$do_age,$do_gender,$do_mailid,$do_state,$do_city,$do_bg,$do_pincode,$do_mobile);


                if($stmt->execute()){
                     //fetching the user back 
 $stmt = $conn->prepare("SELECT do_name,do_age,do_bg,do_mobile,do_pincode,do_gender,do_city,do_state,do_mailid,do_password FROM users WHERE do_name = ?"); 
 $stmt->bind_param("s",$do_name);
 $stmt->execute();
 $stmt->bind_result($id,$do_name,$do_age,$do_gender,$do_mailid,$do_state,$do_city,$do_bg,$do_pincode,$do_mobile,$do_password);
 $stmt->fetch();
 
 $user = array(
 'id'=>$id, 
 'do_name'=>$do_name,
 'do_age'=>$do_age,
 'do_gender'=>$do_gender,
 'do_mailid'=>$do_mailid,
 'do_state'=>$do_state,
 'do_city'=>$do_city,
 'do_bg'=> $do_bg,
 'do_pincode'=>$do_pincode,
 'do_mobile'=>$do_mobile,
 'do_password'=>$do_password
 
 );

        $stmt->close();
 
 $response['error'] = false; 
 $response['message'] = 'User registered successfully'; 
 $response['user'] = $user; 
 }
 }
 
 }else{
 $response['error'] = true; 
 $response['message'] = 'required parameters are not available'; 
 }
            break;
   



        case 'login':
 
 if(isTheseParametersAvailable(array('do_name', 'do_password'))){
 
 $do_name = $_POST['do_name'];
 $do_password = md5($_POST['do_password']); 
 
 $stmt = $conn->prepare("SELECT  do_name,do_age,do_bg,do_mobile,do_pincode,do_gender,do_city,do_state,do_mailid  FROM users WHERE do_name = ? AND do_password = ?");
 $stmt->bind_param("ss",$do_name, $do_password);
 
 $stmt->execute();
 
 $stmt->store_result();
 
 if($stmt->num_rows > 0){
 
 $stmt->bind_result( $id,$do_name,$do_age,$do_gender,$do_mailid,$do_state,$do_city,$do_bg,$do_pincode,$do_mobile,$do_password);
 $stmt->fetch();
 
 $user = array(
 'id'=>$id, 
 'do_name'=>$do_name,
 'do_age'=>$do_age,
 'do_gender'=>$do_gender,
 'do_mailid'=>$do_mailid,
 'do_state'=>$do_state,
 'do_city'=>$do_city,
 'do_bg'=> $do_bg,
 'do_pincode'=>$do_pincode,
 'do_mobile'=>$do_mobile,
 'do_password'=>$do_password
 );
 
 $response['error'] = false; 
 $response['message'] = 'Login successfull'; 
 $response['user'] = $user; 
 }else{
 $response['error'] = false; 
 $response['message'] = 'Invalid username or password';
 }
 }
 break; 
 
 default: 
 $response['error'] = true; 
 $response['message'] = 'Invalid Operation Called';
 }
 
 }else{
 $response['error'] = true; 
 $response['message'] = 'Invalid API Call';
 }
echo json_encode($response);

function isTheseParametersAvailable($params){
 
 foreach($params as $param){
 if(!isset($_POST[$param])){
 return false; 
 }
 }
 return true; 
 }

 ?>
Posted
Updated 20-Dec-21 8:50am
Comments
CHill60 20-Dec-21 3:29am    
Which line of code throws that exception??
zsurya 20-Dec-21 5:06am    
Hi CHill60 ,
above login 'case'
CHill60 20-Dec-21 5:38am    
You are getting that error because you have hard-coded those words if the execute does not work. You would be better off with
$response['message'] = $stmt->error;
At least then you would know what the real error was
Richard MacCutchan 20-Dec-21 4:48am    
You do realise that storing someone's age in a database is totally pointless. And storing a numeric value as a string doubly so.
zsurya 20-Dec-21 5:05am    
Hi Richard,

How to solve the problem please help me , i am fresher for php.



1 solution

Hi,

there seems to be some confusion between GET and POST.
Your code (after proper indentation) basically looks like this:
if (isset($_GET['apicall'])) {
    if ($_GET['apicall']=='signup') {
         if (isset($_POST(all required parms...))) {
             ...
         } else {
             // the error you are getting
         }
    }
}


so it looks like you are GETting the page and nothing is being POSTed.

The normal solution would be to consistently use $_GET (or $_POST, depending on what you want).

FYI: In some cases it could be useful to use $_REQUEST everywhere, which is somewhat like the union of $_GET and $_POST.

:)
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900