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条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题