0

i have been stuck on this error. Appreciate any help on this:

this function is part of a longer code working with ajax. Ajax has no problem reaching into the function, retrieving post data. code returns results with $message and it gets back as to ajax, and data is retrieved as response.message

Error happens the moment it runs into the bind_param.

Tried commenting the codes from bind_param down and ajax returns test messages just fine.

When un-commented bind_param, even with if bind_param fails send message 'fail', else send message 'pass'. nothing gets into the $message.

any ideas to why this happens?

code:

function edit_Loc_Name($connection){//67
    $new_loc_name = mysqli_real_escape_string($connection, $_POST['location_editloc_name']);
    $projid = mysqli_real_escape_string($connection, $_POST['projid']);
    $loc_id = mysqli_real_escape_string($connection, $_POST['edit_loc_id']);
    $checklocname = checkLocationLoc($projid,$new_loc_name,$connection);
    if ($checklocname === "Duplicate location."){
        $message = "Duplicate location.";
    }else if($checklocname === "Location okay"){
        $stmt = $connection->prepare("UPDATE projectlocation SET locname = ? WHERE id = ?");
        if($stmt === false){
            $message = "Ajax err:67 1";$stmt->close();
        }else{
            $stmt->bind_param('si',$new_loc_name,$loc_id);
            $rc = $stmt->execute();
            if($rc === false){
                $message = "Ajax err:67 3";$stmt->close();
            }else{
                $message = "Location updated.";$stmt->close();
            }
        }
    }else{
        $message = "Ajax err:67 5";
    }
    $connection->close();
    return $message;
}
12
  • 3
    And the error is...? Commented Aug 2, 2017 at 8:47
  • mysqli_real_escape_string( is not needed if you use prepared statements Commented Aug 2, 2017 at 8:48
  • nothing happens, i did a message check before the bind_param and commented out the bind_param it is fine. right after i un-commented the bind param, nothing happens Commented Aug 2, 2017 at 8:49
  • are you sure both those variables have correct values? Commented Aug 2, 2017 at 8:50
  • So the page remains white ? Commented Aug 2, 2017 at 8:50

2 Answers 2

2

You are passing the data to real_escape_string (as commented above, it is not needed if you bind later).

This function returns a string, so your $loc_id is a string now.

But in the binding:

$stmt->bind_param('si',$new_loc_name,$loc_id);

You declare the data as si (string, integer) instead of ss.

Try to fix this.

Update:

As an example, I ran this script in my localhost, and it is working properly:

    $connection=mysqli_connect("localhost", "root", "", "my-db");

function edit_Loc_Name($connection){
$loc_id = 1;
$checklocname = "Location okay";    

if ($checklocname === "Duplicate location."){
    $message = "Duplicate location.";
}else if($checklocname === "Location okay"){
    $stmt=$connection->prepare("select * from my-table where id=?");
    if($stmt === false){
        $message = "Ajax err:67 1";//$stmt->close();
    }else{
        $stmt->bind_param('i',$loc_id);
        $rc = $stmt->execute();
        if($rc === false){
            $message = "Ajax err:67 3";//$stmt->close();
        }else{
            $message = "Location updated.";//$stmt->close();
        }
    }
}else{
    $message = "Ajax err:67 5";
}
$connection->close();
return $message;
}
echo edit_Loc_Name($connection)
Sign up to request clarification or add additional context in comments.

8 Comments

@JYoThI but he's treating his loc_id as a string, thefore what Giacomo is saying instead of si he should use ss
loc_id is intended to be an integer. i have removed the real escape string and its still the same problem.
Still assuming that the error comes from there, try to cast the POST value: (int)$_POST['edit_loc_id'] But overall, try to print the error!
Yes, been trying with $message = $stmt->error it's not showing anything, not sure if it's because of bind Parma that caused it not to show or there's just nothing
You are calling the function via ajax, right? What can you see in the console? Do you have ph errors activated? stackoverflow.com/questions/5438060/…
|
0

During multiple function calls, $connection was closed.

checkLocationLoc($projid,$new_loc_name,$connection) had closed the $connection.

Therefore all $connection related methods are not responding.

Once the $connection->close(); is removed, all is working well.

Sorry for the trouble guys. It's a painstaking lesson to me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.