0

I want to Convert this query to pdo : please anybody who can help will be appreciated

function updateonlinesession(){         
if(isset($_SESSION['username']['id'])){
$uid = $_SESSION['username']['id'];
$page = $_SERVER['REQUEST_URI'];
$ip = $_SERVER['REMOTE_ADDR'];
$username = $_SESSION['logged'];

("UPDATE site_user SET dateupdated  = now(),ip = '$ip' WHERE 
username = '".mysql_real_escape_string($_SESSION['username'])."'"); 
}

}
This is What I have tried 
<?php 

function updateonlinesession(){         
if(isset($_SESSION['username']['id'])){
$uid = $_SESSION['username']['id'];
$page = $_SERVER['REQUEST_URI'];
$ip = $_SERVER['REMOTE_ADDR'];
$username = $_SESSION['logged'];

$update =("UPDATE site_user SET dateupdated  = now(),ip = '$ip' WHERE 
username = '"($_SESSION['username'])."'");  
$updated_once= $con->($update);
$updated_once->execute();
}

}
?>
Humphrey
  • 2,659
  • 3
  • 28
  • 38
  • Didn't you ask [this very question](http://stackoverflow.com/questions/15093251/how-to-change-an-update-query-from-mysql-to-pdo) nearly dozen times already? – Your Common Sense Mar 02 '13 at 11:05
  • @YourCommonSense not on update and I asked u a question which I never go no answer . this con is not recognised and I do connect the way u taught me . and I include that connecting page in my pages . it works but it seems it cant be accessed in the function – Humphrey Mar 02 '13 at 11:47

1 Answers1

2

PDO can have values binded to parameters. Like this:

function updateonlinesession(){         
  if(isset($_SESSION['username']['id'])){
    $uid = $_SESSION['username']['id'];
    $page = $_SERVER['REQUEST_URI'];
    $ip = $_SERVER['REMOTE_ADDR'];
    $username = $_SESSION['logged'];
    $update = "UPDATE site_user SET dateupdated  = now(), ip = ? WHERE username = ?");  
    $updated_once = $con->prepare($update);
    $updated_once->bindParam(1, $ip, PDO::PARAM_STR );
    $updated_once->bindParam(2, $username, PDO::PARAM_STR );
    $updated_once->execute();
  }
}
hjpotter92
  • 78,589
  • 36
  • 144
  • 183