【问题标题】:form updating fields that haven't been updated by user - PHP Query用户尚未更新的表单更新字段 - PHP Query
【发布时间】:2019-07-30 11:44:14
【问题描述】:

我的 PHP 查询需要一些帮助。我实际上是让用户有机会在他们登录后更新他们自己的详细信息。表单:

<div class="grid-2"> 
    <p><b>UPDATE MY DETAILS</b></p>
        <form action ="includes/update.inc.php" method ="post">
        <label>S.Name</label>
        <input name="update-surname" type="text" placeholder="Enter new surname...">
        <label>Address</label>
        <input name="update-houseno" type="text" placeholder="Enter house no' or name...">
        <input name="update-ln1" type="text" placeholder="1st Line of Address...">
        <input name="update-town" type="text" placeholder="Town...">
        <input name="update-county" type="text" placeholder="County...">
        <input name="update-postcode" type="text" placeholder="Postcode...">
        <label>Contact Number</label>
        <input name="update-number" type="text" placeholder="Contact Number...">
        <label>Email</label>
        <input name="update-email" type="text" placeholder="Email...">

        <input type="submit" name="update-details" value="Update">
    </form>
</div>

我目前拥有的 php 代码,如果用户没有在框中输入任何内容,它会使用空白输入更新数据库(我不希望发生这种情况),如果没有输入我不想要触摸表格中的那个字段。

    <?php
// Here we check whether the user got to this page by clicking the proper button.
if (isset($_POST['update-details'])) {

      require 'dbh.inc.php';

// We grab all the data which we passed from the signup form so we can use it later.
    $surname = $_POST['update-surname'];
    $houseno = $_POST['update-houseno'];
    $ln1 = $_POST['update-ln1'];
    $town = $_POST['update-town'];
    $county = $_POST['update-county'];
    $postcode = $_POST['update-postcode'];
    $email = $_POST['update-email'];
    $number = $_POST['update-number'];

      // We validate the updated email is correct if email has been updated. 
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    header("Location: ../after-login.php?error=invalidmail=");
    exit();
    }

    $query = "UPDATE `tblMember` SET `fldSName` = '$surname', `fldTelNum` = '$number', `fld1stLnAddress` = '$houseno', `fld2ndLnAddress` = '$ln1', `fld3rdLnAddress` = '$town', `fldCounty` = '$county', `fldPostcode` = '$postcode', `fldEmailAddress` = '$email' WHERE `tblMember`.`fldMemberID` = 1";


    $result = $conn->query($query) or die ("error");
}
?>

一旦加载了 php 表单,网页就会消失,并且不会停留在当前网页上。

所以需要两件事,帮助正确查询和帮助页面变为空白而不停留在网页上。

请注意,我知道这很容易受到注入攻击,我只是想让它在物理上正常工作,然后再尝试了解我如何做准备好的语句。

谢谢!

【问题讨论】:

  • 关于你的第二部分(所以页面不会刷新或重定向)你需要ajax请求。
  • 嗨@pr1nc3,所以页面变为空白,并且在URL中它确实从after-login.php更改为update.inc.php,我将在查询中将标题直接放在after-login.php上页面?
  • 只需将标题放在您的页面中,这样它就知道提交后要做什么。您不能将标题放在查询中,只是在最后做
  • 您可以将具有value 属性的当前值插入到表单输入中,而不是放置空白字段。这样做的好处是可以通知用户当前值是什么,并且您不需要更改查询。对于空白页,更新成功后尝试做header('Location: ../after-login.php')
  • @JoseFG 他/她的查询是正确的

标签: php html phpmyadmin


【解决方案1】:

您需要检查数据输入字段是否为非空/有效。

避免空白字段更新的步骤:

1) 取一个空数组

2) 检查每个发布的变量是否有效,如果有效则将其附加到数组中。

3) 检查数组是否为空。

4) 如果不为空,则触发 SQL。

<?php
// Here we check whether the user got to this page by clicking the proper button.
if (isset($_POST['update-details'])) {

      require 'dbh.inc.php';

// We grab all the data which we passed from the signup form so we can use it later.
    $ln1 = $_POST['update-surname'];
    $houseno = $_POST['update-houseno'];
    $ln1 = $_POST['update-ln1'];
    $town = $_POST['update-town'];
    $county = $_POST['update-county'];
    $postcode = $_POST['update-postcode'];
    $email = $_POST['update-email'];
    $number = $_POST['update-number'];

      // We validate the updated email is correct if email has been updated. 
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    header("Location: ../after-login.php?error=invalidmail=");
    exit();
    }

    $update = [];
    if (! empty($surname)) {
        $update['fldSName'] = "fldSName = '".$surname ."'";
    }

    if (! empty($number)) {
        $update['fldTelNum'] = "fldTelNum='".$number ."'";
    }

    if (! empty($houseno)) {
        $update['fld1stLnAddress'] = "fld1stLnAddress='".$houseno ."'";
    }

    if (! empty($ln1)) {
        $update['fld2ndLnAddress'] = "fld2ndLnAddress='".$ln1 ."'";
    }

    if (! empty($town)) {
        $update['fld3rdLnAddress'] = "fld3rdLnAddress='".$town ."'";
    }

    if (! empty($county)) {
        $update['fldCounty'] = "fldCounty='".$county ."'";
    }
    if (! empty($postcode)) {
        $update['fldPostcode'] = "fldPostcode='".$postcode ."'";
    }
    if (! empty($email)) {
        $update['fldEmailAddress'] = "fldEmailAddress='".$email ."'";
    }


    if (! empty($update)) {
        $query = "UPDATE `tblMember` SET ";
        $query .= implode(', ', $update);
        $query .= " WHERE `tblMember`.`fldMemberID` = 1";
        $result = $conn->query($query) or die ("error");
    }
}
?>

注意:

fldMemberID 似乎是硬编码的。

【讨论】:

  • 嗨@pupil 非常感谢您的帮助。所以我再次在我的update.inc.php 页面中尝试了这段代码,它把我带到了一个带有“错误”字样的新页面。我再次尝试删除我的第一条电子邮件验证行,看看这是否有帮助,但没有运气。有什么简单的方法可以让我特别找出它导致的错误吗? member id = 1 是硬编码的原因,因为我这样做是为了至少获得我项目的基础知识(我知道它应该取决于谁登录)但我还没有到那一步:)
  • @K.Haydock,我已经更新了我的答案。请您现在检查最新的代码。它应该可以工作。
  • 我刚刚重试了您的代码,并在最后一个 if 语句块之后放置了一个标头重定向,效果很好。但是,如果我根本不放入任何电子邮件,它就不喜欢它,我认为这是我靠近页面顶部的代码:'(
  • 如果您不发送电子邮件,您的电子邮件检查在您的查询之前将退出脚本,然后发生其他任何事情。
  • 嗨@pr1nc3,我明白了,所以我应该剪切并粘贴该代码,也许是在关于电子邮件的 if 语句下方? :-) 非常感谢大家的帮助和意见!
【解决方案2】:
<?php
// Here we check whether the user got to this page by clicking the proper button.
if (isset($_POST['update-details'])) {

    require 'dbh.inc.php';

// We grab all the data which we passed from the signup form so we can use it later.
    $surname = $_POST['update-surname'];
    $houseno = $_POST['update-houseno'];
    $ln1 = $_POST['update-ln1'];
    $town = $_POST['update-town'];
    $county = $_POST['update-county'];
    $postcode = $_POST['update-postcode'];
    $email = $_POST['update-email'];
    $number = $_POST['update-number'];

    // We validate the updated email is correct if email has been updated.
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        header("Location: ../after-login.php?error=invalidmail=");
        exit();
    }

    $query = "UPDATE `tblMember` SET ";
    (!empty($surname))?: $query .= "`fldSName` = '$surname',";
    (!empty($houseno))?: $query .= "`fldTelNum` = '$houseno',";
    (!empty($ln1))?: $query .= "`fld1stLnAddress` = '$ln1',";
    (!empty($town))?: $query .= "`fld2ndLnAddress` = '$town',";
    (!empty($county))?: $query .= "`fld3rdLnAddress` = '$county',";
    (!empty($postcode))?: $query .= "`fldCounty` = '$postcode',";
    (!empty($email))?: $query .= "`fldPostcode` = '$email',";
    (!empty($number))?: $query .= "`fldEmailAddress` = '$number'";



   $query .= " WHERE `tblMember`.`fldMemberID` = 1";


    $result = $conn->query($query);

    header("Location: ../after-login.php");  //make sure of the path

}

基本上你是在检查你的输入值,就像你通过连接查询块来构建你的查询一样。

最后添加标题以将您重定向到所需的页面。

【讨论】:

  • 这会导致最后一个逗号的 SQL 语法错误。请检查一下。
  • 已删除,添加新行时忘记了。完美的捕获谢谢
  • 不客气。用户不发fldEmailAddress怎么办? fldPostcode 将是最后一个字段。只是一个好奇的问题。不要怀疑你的答案:)
  • 然后根据代码退出验证邮件的上一次检查
  • 嗨@pr1nc3 在删除我的第一个电子邮件验证检查后(如果框留空,它不喜欢在那里)所以我尝试了这段代码并在更新按钮上单击它确实将我重定向到同一页面after-login.php 但是它似乎没有更新我的任何表格。我现在再检查一次
【解决方案3】:

对于第一个问题,您可以将查询编辑为

UPDATE tblMember
SET fldSName = IF('$surname' = '', fldSName, '$surname'),
    fldTelNum = IF('$number' = '', fldTelNum, '$number'),
    fld1stLnAddress = IF('$houseno' = '', fld1stLnAddress, '$houseno'),
    fld2ndLnAddress = IF('$ln1' = '', fld2ndLnAddress, '$ln1'),
    fld3rdLnAddress = IF('$town' = '', fld3rdLnAddress, '$town'),
    fldCounty = IF('$county' = '', fldCounty, '$county'),
    fldPostcode = IF('$postcode' = '', fldPostcode, '$postcode'),
    fldEmailAddress = IF('$email' = '', fldEmailAddress, '$email'),
WHERE  
    `tblMember`.`fldMemberID` = 1  

对于第二个问题,您必须删除 die() 并重定向到 after-login.php as

 $conn->query($query);
 header("Location: ../after-login.php");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    相关资源
    最近更新 更多