Using Session Variable in SQL query

Hi guys, I have a problem with this query.

if(isset($_POST['button']) && $_POST['button'] == 'submitReport')
{
	
$conn = DatabaseManager::getConnection();
try
{
$sql = "INSERT INTO report (serviceName, branchId, branchName)  VALUES (:serviceName,  :'".$_SESSION['branchId']."', :'".$_SESSION['branchName']."')";

$s = $conn->prepare($sql);
$s->bindValue(':serviceName', $_POST['serviceName']);
$s->bindValue(':branchId', $_SESSION['branchId']);
$s->bindValue(':branchName', $_SESSION['branchName']);
$s->execute();
$conn = null;
}
catch (PDOException $e)
{
$error = 'There was an error while submitting the new service detail, Please try again later.' . $e->getMessage();
include 'error.html.php';
exit();

}
if($s->rowCount() == 1)
  {
  $output = 'New service detail successfully submitted';
  include 'output.html.php';
  exit();
  }
else
{
$error = 'Unable to perform your request now, Please try again later.';
include 'error.html.php';
exit();
       }

It keeps giving me this error code: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens.
What am I doing wrong? Please note that the two session variables used are actively working on their respective page of usage. I’m suspecting the usage of the session variables in terms of binding the value. I need help please because the query works when session variables weren’t used. Also, please note that it is the index page has the code above and processes the information from the report form on another page. thanks.

NB: I have session_start() running actively on all my pages.

I believe you want this instead


$sql = "INSERT INTO report (serviceName, branchId, branchName)  VALUES (:serviceName,  :branchId, :branchName)";

You put the placeholder names in there and bind the actual values later on.

Change this bit

$sql = "INSERT INTO report (serviceName, branchId, branchName)  VALUES (:serviceName,  :branchId, :branchName)"; //this line

$s = $conn->prepare($sql); 
$s->bindValue(':serviceName', $_POST['serviceName']); 
$s->bindValue(':branchId', $_SESSION['branchId']); 
$s->bindValue(':branchName', $_SESSION['branchName']); 
$s->execute(); 
$conn = null; 

to this (actually lines with changes marked with comments)

$sql = "INSERT INTO report (serviceName, branchId, branchName)  VALUES (:serviceName,  :branchId, :branchName)"; // and this line

$s = $conn->prepare($sql); 
$s->bindValue(':serviceName', $_POST['serviceName']); 
$s->bindValue(':branchId', $_SESSION['branchId']); 
$s->bindValue(':branchName', $_SESSION['branchName']); 
$s->execute(); 
$conn = null; 

I want to appreciate you guys. your reply was sooooooo helpful. I’ve learnt something new. Thanks