title: PHP changing old mysql_query to PDO
body: <p>I have some old mysql_query queries in my code which i want to convert in to PDO but am struggling to get to work.</p>
  
  <p>my original code was:</p>
  
  <pre><code>mysql_query("UPDATE people SET price='$price', contact='$contact', fname='$fname', lname='$lname' WHERE id='$id' AND username='$username' ")
  or die(mysql_error()); 
  </code></pre>
  
  <p>now i am trying:</p>
  
  <pre><code>$sql = "UPDATE people SET price='$price', contact='$contact', fname='$fname', lname='$lname' WHERE id='$id' AND username='$username'";
  $q   = $conn-&gt;query($sql) or die("failed!");
  </code></pre>
  
  <p>but can't seem to get it to work, any ideas?</p>
  
  <p>UPDATED CODE:</p>
  
  <pre><code>$conn = new PDO("mysql:host=$host;dbname=$db",$user,$pass);
  
  
   // check if the form has been submitted. If it has, process the form and save it to the   database
   if (isset($_POST['submit']))
   { 
   // confirm that the 'id' value is a valid integer before getting the form data
   if (is_numeric($_POST['id']))
    {
   // get form data, making sure it is valid
   $id = $_POST['id'];
   $fname = mysql_real_escape_string(htmlspecialchars($_POST['fname']));
   $lname = mysql_real_escape_string(htmlspecialchars($_POST['lname']));
   $contact = mysql_real_escape_string(htmlspecialchars($_POST['contact']));
   $price = mysql_real_escape_string(htmlspecialchars($_POST['price']));
  
  
   // check that firstname/lastname fields are both filled in
   if ($fname == '' || $lname == '' || $contact == '' || $price == '' )
   {
   // generate error message
   $error = 'ERROR: Please fill in all required fields!';
  
   //error, display form
   renderForm($id, $fname, $lname, $contact, $price, $error);
   }
   else
   {
   // save the data to the database
   $username = $_SESSION['username'];
  
   $query = "UPDATE people 
           SET price=?, 
               contact=?, 
               fname=?, 
               lname=? 
            WHERE id=? AND 
                  username=?";
  $stmt = $db-&gt;prepare($query);
  $stmt-&gt;bindParam(1, $price);
  $stmt-&gt;bindParam(2, $contact);
  $stmt-&gt;bindParam(3, $fname);
  $stmt-&gt;bindParam(4, $lname);
  $stmt-&gt;bindParam(5, $id);
  $stmt-&gt;bindParam(6, $username);    
  $stmt-&gt;execute();
  
  
   // once saved, redirect back to the view page
  header("Location: view.php"); 
  }
  </code></pre>