ak47 Posted June 26, 2017 Share Posted June 26, 2017 (edited) Hi All, I am new to php and I don't about following error. So I request you to give me solution for this and also please explain something about this error. Here I am working on User's profile page. I able to fetch data and displaying in profile's form, but data is not updating and I got the following error when I clicked for "Updation". " Uncaught Error: Call to a member function bindParam() on boolean in C:\xampp\htdocs\VS\components\update-profile.php:16 Stack trace: #0 {main} thrown in C:\xampp\htdocs\VS\components\update-profile.php on line 16 " And my code is: <?php include '../_database/database.php'; ini_set("display_errors",1); session_start(); $msg=""; $temp=(isset($_SESSION['Username']) ? $_SESSION['Username'] : ''); $id=(isset($_SESSION['Id']) ? $_SESSION['Id'] : ''); if($_SERVER["REQUEST_METHOD"] == "POST") { $username = $_POST['Username']; $email = $_POST['Email']; $employee = $_POST['EmployeeID']; $designation = $_POST['Designation']; $password = $_POST['Password']; $sql=$database->prepare("update set Username=:name,Email=:email,EmployeeID=:employee,Designation=:designation,Password=:password where Id='$id'"); $sql->bindParam(':Username',$name,PDO::PARAM_STR, 50); $sql->bindParam(':Email',$email,PDO::PARAM_STR, 50); $sql->bindParam(':EmployeeID',$employee,PDO::PARAM_INT, 11); $sql->bindParam(':Designation',$designation,PDO::PARAM_STR, 50); $sql->bindParam(':Password',$password,PDO::PARAM_STR, 50); if($sql->execute()){ echo "<font face='Verdana' size='2' color=green>You have successfully updated your profile<br></font>"; } else{ print_r($sql->errorInfo()); $msg=" <font face='Verdana' size='2' color=red>There is some problem in updating your profile. Please contact site admin<br></font>"; } } ?> If this is wrong kindly give me a solution. Thank You, Edited June 26, 2017 by ak47 Link to comment Share on other sites More sharing options...
ak47 Posted June 26, 2017 Author Share Posted June 26, 2017 Sorry, I have made some mistakes in my previous post. And my corrected code is: <?php include '../_database/database.php'; ini_set("display_errors",1); session_start(); $msg=""; $temp=(isset($_SESSION['Username']) ? $_SESSION['Username'] : ''); $id=(isset($_SESSION['Id']) ? $_SESSION['Id'] : ''); if($_SERVER["REQUEST_METHOD"] == "POST") { $username = $_POST['Username']; $email = $_POST['Email']; $employee = $_POST['EmployeeID']; $designation = $_POST['Designation']; $password = $_POST['Password']; $sql=$database->prepare("UPDATE users SET Username=:name,Email=:email,EmployeeID=:employee,Designation=:designation,Password=:password WHERE Id='$id'"); $sql->bindParam(':username',$username,PDO::PARAM_STR, 50); $sql->bindParam(':email',$email,PDO::PARAM_STR, 50); $sql->bindParam(':employee',$employee,PDO::PARAM_INT, 11); $sql->bindParam(':designation',$designation,PDO::PARAM_STR, 50); $sql->bindParam(':password',$password,PDO::PARAM_STR, 50); if($sql->execute()){ echo "<font face='Verdana' size='2' color=green>You have successfully updated your profile<br></font>"; } else{ print_r($sql->errorInfo()); $msg=" <font face='Verdana' size='2' color=red>There is some problem in updating your profile. Please contact site admin<br></font>"; } } ?> But still I have the error: "Fatal error: Uncaught Error: Call to a member function bindParam() on boolean in C:\xampp\htdocs\VS\components\update-profile.php:16 Stack trace: #0 {main} thrown in C:\xampp\htdocs\VS\components\update-profile.php on line 16" Link to comment Share on other sites More sharing options...
dsonesuk Posted June 26, 2017 Share Posted June 26, 2017 what does $database variable ref to exactly? Link to comment Share on other sites More sharing options...
justsomeguy Posted June 26, 2017 Share Posted June 26, 2017 If $sql is a boolean (true or false), then that probable means that your call to prepare failed. You should check if prepare worked before trying to run the other code. You should also make Id another placeholder in that prepared statement. Link to comment Share on other sites More sharing options...
ak47 Posted June 28, 2017 Author Share Posted June 28, 2017 (edited) Hi everyone, thank you for your valuable time you spent on my post. But I didn't wanted to waste your time, so I tried my best. But still I have error "Fatal error: Call to a member function bind_param() on boolean". Kindly give me a solution to get rid-off this. Below is my code: <?php include '../_database/database.php'; ini_set("display_errors",1); session_start(); $msg=""; if($_SERVER["REQUEST_METHOD"] == "POST") { $username = $_POST['Username']; $email = $_POST['Email']; $employee = $_POST['EmployeeID']; $designation = $_POST['Designation']; $password = $_POST['Password']; $id = $_SESSION['Id']; $sql=$database->prepare("UPDATE users SET (Username,Email,EmployeeID,Designation,Password) VALUES (?,?,?,?,?) WHERE Id=?"); $sql->bind_param("ssiss", $username, $email, $employee, $designation, $password); $sql->execute(); if($sql->execute()){ echo "<font face='Verdana' size='2' color='green'>You have successfully updated your profile<br /></font>"; } else{ print_r($sql->errorInfo()); $msg=" <font face='Verdana' size='2' color='red'>There is some problem in updating your profile. Please contact site admin<br /></font>"; } } ?> include '../_database/database.php'; ---- Here I defined database as " $database=mysqli_connect("localhost", "root", "", "videos") or die(mysqli_error()); Edited June 28, 2017 by ak47 missed with something. Link to comment Share on other sites More sharing options...
dsonesuk Posted June 28, 2017 Share Posted June 28, 2017 You have not bound a parameter for the WHERE statment, for every ? There must be a type and variable/value defined in the same order. 1 Link to comment Share on other sites More sharing options...
ak47 Posted June 28, 2017 Author Share Posted June 28, 2017 @dsonesuk, I really don't know how to do that. If you don't mind can you help me to fix that by writing the script? Link to comment Share on other sites More sharing options...
Gabrielphp Posted June 28, 2017 Share Posted June 28, 2017 (edited) It is so much much easier to do it in PDO, why don't you use it ? And it is even more secure. dbconnection: try { $username = "db_username"; $password = "db_password"; $db = new PDO("mysql:host=localhost;dbname=your_dbname", $username, $password); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected !"; } catch (PDOException $e) { echo $e->getMessage(); } then your script to insert those values: try { $sql = "UPDATE users SET Username=:username, Email=:email, EmployeeID=:empid, Designation=:design, Password=:password WHERE Id = :id"; $stmt = $db->prepare($sql); $stmt->bindParam(":username", $username); $stmt->bindParam(":email", $email); $stmt->bindParam(":empid", $employee); $stmt->bindParam(":design", $designation); $stmt->bindParam(":password", $password); $stmt->bindParam(":id", $id); if($stmt->execute()){ echo "<font face='Verdana' size='2' color='green'> You have successfully updated your profile <br /> </font>"; } else { $msg = "<font face='Verdana' size='2' color='red'> There is some problem in updating your profile. Please contact site admin <br /></font>"; } } catch (PDOException $e) { print_r($e->getMessage()); } This is a clean way to do what you want but in PDO not MySQLi. Edited June 28, 2017 by Gabrielphp 1 Link to comment Share on other sites More sharing options...
justsomeguy Posted June 28, 2017 Share Posted June 28, 2017 If you're going to ask the same question, then it's going to be the same answer: Quote If $sql is a boolean (true or false), then that probable means that your call to prepare failed. You should check if prepare worked before trying to run the other code. If you don't want to use PDO and have it throw exceptions when you make a mistake, then you need to check for errors yourself. If prepare returned false, then you need to print the error message from MySQL. You have a problem with the SQL query you're trying to prepare. You changed the format of that UPDATE query between the two pieces of code and now the query is wrong. UPDATE queries do not use the same format as INSERT queries. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now