dongliang1996 2016-03-22 11:12
浏览 36

如何使用PDO更新行?

How do I turn this code into a code that works with PDO?

if($_POST['name']=='home_title'){
$id=$_POST['pk'];
$home_title=$_POST['value'];
$result=mysql_query("SELECT COUNT(*) as count FROM sw_configuration WHERE id=$id") or die(mysql_error());
$count= mysql_fetch_row($result);
if($count[0]==0){
   mysql_query("INSERT INTO sw_configuration(id,home_title) VALUES('".$id."','".$home_title."')") or die(mysql_error()); 
}else{
   mysql_query("UPDATE sw_configuration SET home_title='".$home_title."' WHERE id=$id") or die(mysql_error()); 
  }
}
  • 写回答

1条回答 默认 最新

  • douqilin4296 2016-03-22 11:36
    关注

    Simply using PDO is not enough to make your script secure from SQL injection attacks. You still need to make sure all of your user-supplied variables are either properly quoted or better yet, use prepared statements with bound parameters, which is the preferred way. If you don't do this, there is no difference security-wise between using PDO and the regular mysql_* functions. Here is an example of how you can do it:

    $stmt = $dbh->prepare("INSERT INTO sw_configuration(id,home_title) 
        VALUES(:id,:home_title)");
    $stmt->bindValue(':id', $id);
    $stmt->bindValue(':home_title', $home_title);
    $stmt->execute();
    

    Or if you need to select a value:

    $stmt = $dbh->prepare("SELECT COUNT(*)
        FROM sw_configuration WHERE id = :id");
    $stmt->bindValue(':id', $id);
    $stmt->execute();
    
    $count = $stmt->fetchColumn();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题