【问题标题】:mysql update query by post email and client [closed]通过邮件和客户端的mysql更新查询[关闭]
【发布时间】:2019-11-15 15:54:50
【问题描述】:

我的数据库更新确实有问题:我需要使用 $_post 表单中的数据更新数据库,但它不会更新。

if(isset($_POST["submit"]))
{
$query = "SELECT COUNT(*) AS count FROM newsletter WHERE client = ? AND email = ?";
$sql = $dbp->prepare($query);
$sql->execute(array($_POST["client"], $_POST["email"]));
$count = $sql->fetch(PDO::FETCH_ASSOC);
if($count["count"] == "0")
{
  $query = "UPDATE newsletter SET tip = 'subscriber' WHERE client = ? AND email = ?";
  $sql = $dbp->prepare($query);
  $sql->execute(array($_POST["client"], $_POST["email"]));
}
echo '<h4>congrats! '.$_POST["email"].'</h4>';
exit;
}

以及 $_POST 数据的表单代码。

 <form method="post" action="?">

            <div>
                <div style="display:inline-block;margin-bottom:20px;"><input type="checkbox" name="submit" value="Confirm"  required> <span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size:12px;">confirm</span></div>
                <div style="display:inline-block;"><input type="submit" name="submit" value="Confirm"/></div>
            </div> 
            <input type="hidden" name="email" value="<?php echo $_GET["email"]; ?>" />
          <input type="hidden" name="client" value="<?php echo $_GET["client"]; ?>" />
        </form>

【问题讨论】:

  • 您有 2 个不同的输入具有相同的名称。
  • 我改了,还是不行
  • 拥有 SELECT 似乎没有任何意义
  • 然后查看(新)答案。
  • else { echo "Sorry"; } 添加到if($count["count"] == "0") {...} 时会发生什么?它去那里吗?

标签: php mysql forms input


【解决方案1】:

您的问题在于这里的逻辑执行:

if($count["count"] == "0")
{
     $query = "UPDATE newsletter SET tip = 'subscriber' WHERE client = ? AND email = ?";

如果计数为 0,则没有要更新的行。计数为0时需要插入。如果要更新,需要有一行要更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多