更新数据Php pdo

时间:2016-04-20 16:14:35

标签: php mysql pdo

我试图使用PHP更新我的数据,但它不起作用,任何想法?  这是代码,这不是完整的代码(它没有完成),但即使用户名也无法更新。

<?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";
   ?>

1 个答案:

答案 0 :(得分:0)

我猜这行中的语法错误

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

校正的

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