dongzhao3040 2015-11-19 07:38
浏览 148
已采纳

将php切换列值设置为1或0

I'm learning PHP and getting a little frustrated. I have an html form that is sending data to another php page $_POST["id"];.

On a 2nd php page I'm trying have the "available" column in the table either switch to 1 or 0. If it's already 1 go to 0, and if it's 0 go to 1.

I know my code is probably completely wrong and messy but please excuse me as I'm still learning.

if ($row["available"] == 1) {
    //$row["available"] = 0;
    $sql = "UPDATE check_in_out SET available=0 WHERE id='".$_POST["id"]."'";
    if ($conn->query($sql) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }
} else {
    //$row["available"] = 1;
    $sql = "UPDATE check_in_out SET available=1 WHERE id='".$_POST["id"]."'";
    if ($conn->query($sql) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }
}
  • 写回答

1条回答 默认 最新

  • dragon19720808 2015-11-19 07:43
    关注

    If you want to toggle the value you can use this:

    $sql = "UPDATE check_in_out SET available = (1-available) WHERE id='".$_POST["id"]."'";
    

    or

    $sql = "UPDATE check_in_out SET available = IF(available = 0, 1, 0) WHERE id='".$_POST["id"]."'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度