-1

Im trying to update my data using php but it doesnt work, any ideas? This is the code, this isnt the full code (its not done) but even the username cant be updated.

<?php  
 session_start();
 include "dbconfig.php";
 require "check.php";

 if(!empty($_POST['user_name']) || !empty($_POST['user_email'])){

            $user_name = trim($_POST['user_name']);
            $user_email = trim($_POST['user_email']);

 $count=$db_con->prepare("SELECT * FROM users WHERE user_id=:userid");
 $count->bindParam(":userid",$_SESSION['user_session'],PDO::PARAM_STR,15);
 $count->execute();
 $row = $count->fetch(PDO::FETCH_OBJ);

 $sql=$db_con->prepare("update users set user_name=:username where user_id='$row->user_id'");
 $sql->bindParam(':username',$user_name,PDO::PARAM_STR, 32);

 if($sql->execute()){
 echo "Successfully updated Profile";
 }
 else{
 print_r($sql->errorInfo()); 

 }
  else {

    echo "No data inserted!"
   }


   include "home.php";
   ?>
Big A
  • 31
  • 4
  • Are you getting an error? Have you checked your logs? – acupofjose Apr 20 '16 at 16:16
  • No I dont get any errors – Big A Apr 20 '16 at 16:17
  • So you aren't reaching your `print_r` statement and throwing an error.. is your script returning any information at all? Usually your apache logs would show something. – acupofjose Apr 20 '16 at 16:18
  • It doesnt return any information – Big A Apr 20 '16 at 16:37
  • Set PDO in exception mode (it's in php manual), re-run your queries, use `bindValue` instead of `bindParam` (it isn't the cause of errors, but the two functions are a bit different and you don't need `bindParam`, it's rarely required). Now, after setting PDO in exception mode, you'll receive exceptions, so you can proceed with debugging. – N.B. Apr 20 '16 at 16:45

1 Answers1

0

I guess a syntax error in this Line

$sql=$db_con->prepare("update users set user_name=:username where user_id='$row->user_id'");

Corrected

$sql=$db_con->prepare("update users set user_name=:username where user_id="$row->user_id);
Prince
  • 291
  • 1
  • 2
  • 9
  • Perhaps the corrected query would be binding the `user_id = :user_id` as well, instead of passing value from the object. – N.B. Apr 20 '16 at 16:21