dongyongju9560 2011-11-21 19:50
浏览 23
已采纳

只有我的If语句被激活,而不是我的其他,即使我没有为id提供输入

Here's my code, for some reason, whether I specify a value for the input id or not, it seems to provide one, because ONLY the if statement is run. Also, when I provide a value for the id (or not), nothing is updated or changed.

<?php
$culture = $_POST["culture"];
if (isset($_POST["id"])) {
    $UID = $_POST["id"];
    mysql_query("UPDATE culture SET cult_desc='$culture' WHERE cult_id=$UID");
    echo $UID . "  " . $culture . " If Statement Activated";
}
else {
     mysql_query("INSERT INTO culture
             VALUES(cult_desc='$culture')");
             echo $culture . " Else Statement Activated";
}
?>

Here's the html code:

    <form action="addculture.php" method="POST">
          <span><input type="text" size="3" name="id" />
          <input type="text" name="culture" />
        <input type="submit" value="add" /></span>
    </form>
  • 写回答

2条回答 默认 最新

  • douhe6255 2011-11-21 19:56
    关注

    If your ID value comes from an <input> field in the browser, then isset($_POST['fieldname']) will ALWAYS be true. An input field is always submitted the server, even if there is no data in it.

    If you want to check if there's something in the field, then do this:

    if (isset($_POST['id']) && ($_POST['id'] !== '')) {
       ...
    }
    

    That'll check if the id field actuall exists (which is should), and if it contains something OTHER than an empty string - all data coming out of $_POST/$_GET is a string, even if it's numeric data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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