-1

I have encode $result in one function and try to decode and match with a condition in another function. Here is the code:

function postOperation(){
    include("function/db.php");
        $name = $_POST['name'];
        $mfsname = $_POST['mfsname'];
        $mfsnumber = $_POST['mfsnumber'];
        $amount = $_POST['amount'];
        $pin_no = $_POST['pinno'];

        $insert_info = "insert into table_name(name, mfs_name, mfs_number, amount, pin_no) 
        values('$name', '$mfsname', '$mfsnumber', '$amount', '$pin_no')";

        $success=0;
        $insert_final = mysqli_query($conn,$insert_info);
        if($insert_final){
           $success=1;
           $result["success"]=$success;
        }
        json_encode($result);
}

function getOperation(){
        $array = json_decode($result, true);
        if($array["success"] == 1){
           echo "<script>alert('Transaction successful!')</script>";
        }

}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    You need to return `$result` from `postOperation` and pass it to `getOperation` (or declare it a global in both functions...) – Nick Mar 04 '20 at 06:49
  • What is the result you're experiencing? What is the expected result? Please add some clarification so others can better help you. – tfrascaroli Mar 04 '20 at 07:52
  • The statement `json_encode($result);` does not have any effect. It encodes the value of `$result` as JSON and drops the encoded value. You should either store it in a variable or, if you want to access it outside the function let the function return it. – axiac Mar 04 '20 at 09:31
  • Your code is vulnerable to SQL injection. You should use prepared statements. – Dharman Mar 04 '20 at 09:38

1 Answers1

0

There are some logical error

$success=0; $result["success"]=$success; //I have added this line

and added a return json_encode($result);

See the full code:

function postOperation(){
        include("function/db.php");

        $name = $_POST['name'];
        $mfsname = $_POST['mfsname'];
        $mfsnumber = $_POST['mfsnumber'];
        $amount = $_POST['amount'];
        $pin_no = $_POST['pinno'];

        $stmt = mysqli_prepare("insert into table_name(name, mfs_name, mfs_number, amount, pin_no) values(?, ?, ?, ?, ?)");
        mysqli_stmt_bind_param($stmt, "ssiii", $name, $mfsname, $mfsnumber, $amount, $pin_no);

        $success=0;
        $result["success"]=$success; 
        if(mysqli_stmt_execute($stmt)){
           $success=1;
           $result["success"]=$success;
        }

        return json_encode($result);
}

function getOperation($result){
        $array = json_decode($result, true);
        if($array["success"] == 1){
           echo "<script>alert('Transaction successful!')</script>";
        }

}

Please test the above function after hardcoding some values for $_POST variables. Then call those functions like below:-

$result = postOperation(); 
getOperation($result);

And another important thing: Your sql statement is not safe. There will be SQL injection Please refer the link : How to avoid SQL injection

MjM
  • 571
  • 4
  • 15
  • You forgot to add a parameter to the `getOperation()` function definition. – El_Vanja Mar 04 '20 at 07:54
  • @EI_Vanja, I have mentioned to hard code some values for `$_POST` in function `getOperation()`, so there is no need to pass parameters to the function `getOperation()` – MjM Mar 04 '20 at 08:10
  • In your proposed answer you are passing a parameter to it (`getOperation($result)`) and inside the function you're doing something with that variable (`$array = json_decode($result, true)`), but you didn't define the function taking a parameter (`function getOperation(){`). This will result in an undefined variable error. – El_Vanja Mar 04 '20 at 08:49