drx3157 2012-08-21 22:20
浏览 42
已采纳

如何使用WHERE使用pdo将内容插入到mysql中

So I am trying to change the status of something when values are met using WHERE

Code:

$insertstatus = $DBH->prepare("INSERT INTO
   csvdata (status)  VALUES ('$status') WHERE username = '".$username."'");
        $insertstatus->execute();

Not working. If you could give me a hand.

Thank you for your time!

  • 写回答

1条回答 默认 最新

  • douci2015 2012-08-21 22:21
    关注

    If you want to use the where clause, you need to use update. From the looks of it, you are trying to update anyhow as you are only using one column from your table.

    $insertstatus = $DBH->prepare("update
       csvdata set status=  '$status' WHERE username = '".$username."'");
        $insertstatus->execute();
    

    As PeeHaa correctly points out though, using a prepared statement with parameters would be a slight change in your code, and a better option for you. You can do it like this:

    $sql="update csvdata set status=:status where username=:username";
    $sth=$DBH->prepare($sql);
    $sth->execute(array(':status' => $status, ':username' => $username));
    

    This way you are preparing the statement so the database knows what will happen. You then pass the variables to the database via the execute() function in an array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题