SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using Session Variable in SQL query

    Hi guys, I have a problem with this query.

    PHP Code:
    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.

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I believe you want this instead
    PHP Code:
    $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.
    - Robert

  3. #3
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,068
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Change this bit

    PHP Code:
    $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)

    PHP Code:
    $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
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to appreciate you guys. your reply was sooooooo helpful. I've learnt something new. Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •