首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >模式不能使用PHP进行验证

模式不能使用PHP进行验证
EN

Stack Overflow用户
提问于 2018-07-27 05:46:32
回答 1查看 115关注 0票数 0

Modal不会触发我的PHP验证。我不能让我的模式使用我的PHP验证。HTML验证有效。它连接到数据库并进行更新,但跳过服务器端验证。我非常感谢任何人的帮助,这对我来说是新的,为什么一个模式不能验证。

我的PHP和Modal表单在一个文件中。我知道我需要在我的表单中添加更多的错误消息,但是保留电话号码和名字不会触发任何错误消息,所以我会在修复此问题后添加更多的错误消息。代码只是执行并更改数据库。

       <?php
//ini_set( 'display_errors', 1 );
//error_reporting( E_ALL );
session_start();

  // Include db config
require_once 'includes/dbh.inc.php';




  // Process form when post submit
// if($_SERVER['REQUEST_METHOD'] === 'POST'){
    // Sanitize POST
    $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

    $error=array();
//    $error = false;
//    $error = isset($_SESSION['error']);
//      $error = $_SESSION['error'];

    function test_input($data)
    {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }

    $first  = test_input($_POST['first']);
    $last   = test_input($_POST['last']);
    $email  = test_input($_POST['email']);
    $unit   = $_SESSION['unit'];
    $bed    = test_input($_POST['bed']);
    $bath   = test_input($_POST['bath']);
    $web    = test_input($_POST['web']);
    $phone  = test_input($_POST['phone']);
    $manage = test_input($_POST['manage']);
    $ck         = $_POST['rent'];
    $id     = $_SESSION['id'];

    // Validate name
    if(empty($first) || empty($last)){
      $error['name'] = "Enter name";
    }

    // Validate email
    if(empty($email)){
      $error['email'] = "Please enter email";
      }
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $error['email'] = "Invalid email format";
    }
        // Validate name
    if(empty($bed) || empty($bath)){
      $error['rooms'] = 'Please enter bed/bath #';
    }
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$web)) {
        $error['web'] = "Invalid URL";
      }
    // Validate name
    if(empty($phone)){
    $error['phone'] = 'Please enter your phone';
  }
    //Check phone # format 000-000-0000
    if(!preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone)) {
      $error['phone'] = 'Please enter correct phone format';
  }
    //Check manage format
    if (!preg_match("/^(\s\(([^)]+)\))?[[:punct:]]?\p{Lu}+(?:[\s'-]?[\p{L}\d]+)+(\(([^)]+)\))*$/", $manage)) {
      $error['manage'] = 'Please enter correct management format';
  }

  if (empty($error)) {
    var_dump($error);
    print_r($error);

   $sql = 'UPDATE condos_hp SET user_firstname=:first, user_lastname=:last, user_email=:email, user_bed=:bed, user_bath=:bath, user_web=:web, user_phone=:phone, user_manage=:manage, rent=:rent WHERE id=:id';

      // Prepare statement
     $stmt = $pdo->prepare($sql);
        // Bind params
        $stmt->bindParam(':first', $first, PDO::PARAM_STR);
        $stmt->bindParam(':last', $last, PDO::PARAM_STR);
        $stmt->bindParam(':email', $email, PDO::PARAM_STR);
//        $stmt->bindParam(':unit', $unit);
        $stmt->bindParam(':bed', $bed, PDO::PARAM_INT);
        $stmt->bindParam(':bath', $bath, PDO::PARAM_INT);
        $stmt->bindParam(':web', $web, PDO::PARAM_STR);
        $stmt->bindParam(':phone', $phone, PDO::PARAM_STR);
        $stmt->bindParam(':manage', $manage, PDO::PARAM_STR);
        $stmt->bindParam(':rent', $ck, PDO::PARAM_INT);
        $stmt->bindParam(':id', $id, PDO::PARAM_INT);

        $stmt->execute(array(':first'=>$first, ':last'=>$last, ':email'=>$email, ':bed'=>$bed, ':bath'=>$bath, ':web'=>$web, ':phone'=>$phone, ':manage'=>$manage, ':rent'=>$ck, ':id'=>$id));


//        if($stmt){
//   header('Location: index1.php');
//}

if (isset($_POST['update'])) {
//     session_start();
      //Then delete all SESSION variables
     session_unset();
      //And destroy the current session that is running
      session_destroy();
    //  exit(header("Location: http://www.harbour-place.com/login0.php?update=success"));

        if (headers_sent()) {
    die("Redirect failed. Please click on this link: <a href=http://www.harbour-place.com/login0.php>");
}
else{
   exit(header("Location: http://www.harbour-place.com/login0.php?update=success"));
}
}
}

include_once 'includes/mailer2.php';
?>


    <!-- Button trigger modal -->
    <div class="row justify-content-center">
   <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#exampleModalCenter">
   UPDATE
   </button>
   </div>
</div>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="userupdateform" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalCenterTitle">UPDATE RENTAL UNIT # <?php echo htmlentities($_SESSION['unit']) ?> </h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
              <div class="form-row justify-content-center">

                <div class="form-group col-md-4">
              <label >First Name</label>
              <input type="text" class="form-control form-control-sm <?php echo (!empty($error['name'])) ? 'is-invalid' : ''; ?>" name="first" value="<?php echo htmlentities($_SESSION['first']) ?>">
              <span class="invalid-feedback"><?php echo $error['name']; ?></span>
                </div>
                <div class="form-group col-md-4">
              <label >Last Name</label>
                  <input type="text" class="form-control form-control-sm" name="last" value=<?php echo  htmlentities($_SESSION['last']) ?> required>
                </div>
              </div>
                <div class="form-row justify-content-center">
                <div class="form-group col-md-8">
              <label >Email</label>
                  <input type="text" class="form-control form-control-sm"  name="email" value=<?php echo    htmlentities($_SESSION['email']) ?> required>
                </div>
                <!--<div class="form-group col-md-4">
              <label >Unit</label>
                  <input type="text" class="form-control form-control-sm" name="unit" value=<php echo   $_SESSION['u_unit'] ?> required>
            </div>-->
          </div>
                <div class="form-row justify-content-center">
                    <div class="form-group col-md-2">
              <label >Bed</label>
                  <input type="text" class="form-control form-control-sm" name="bed" value=<?php echo   htmlentities($_SESSION['bed']) ?> required>
                </div>
            <div class="form-group col-md-2">
              <label >Bath</label>
              <input type="text" class="form-control form-control-sm" name="bath" value=<?php echo  htmlentities($_SESSION['bath']) ?> required>
            </div>
            <div class="form-group col-md-4">
              <label >Web Site - www.myunit.com</label>
             <input type="text" class="form-control form-control-sm"  name="web"  value=<?php echo htmlentities($_SESSION['web']) ?> required>
           </div>
              </div>
          <div class="form-row justify-content-center">
                    <div class="form-group col-md-3">
                <label >Phone 000-000-0000</label>
                  <input type="text" id="yourphone2" class="form-control form-control-sm <?php echo (!empty($error['phone'])) ? 'is-invalid' : ''; ?>" name="phone" value="<?php echo htmlentities($_SESSION['phone']) ?>">
              <span class="invalid-feedback"><?php echo $error['phone']; ?></span>
                </div>
                    <div class="form-group col-md-5">
              <label >Managment Co (VRBO, Self,etc)</label>
              <input type="text-capitalize" class="form-control form-control-sm" name="manage" value="<?php echo htmlentities(  $_SESSION['manage']) ?>" required>
                </div>  </div>
            <!--<div class="form-row justify-content-center">
                    <div class="form-group col-md-8">
                  <label >Managment Co - VRBO etc.</label>
                  <input type="text" class="form-control form-control-sm" name="manage" value="" required>
                </div></div>-->
              <div class="form-row justify-content-center">
                    <div class="custom-control custom-checkbox">
             <input type="hidden" name="rent" value="0">
             <input type="checkbox" value="1" name="rent" class="custom-control-input" id="customCheck1" checked="checked">
             <label class="custom-control-label" for="customCheck1">Check to show on the rental site</label>
             </div></div>
            <p class="text-center mb-1"><small><i class="fas fa-lock"></i>Your Information is Safe With us!<br> You will need to re-login after clicking Update<br>This also updates the HP rental website listing. </small></p>

                <button type="submit" class="btn btn-primary btn-sm" name="update">UPDATE HP UNIT</button>
            </form>

      </div>

    </div>
  </div>
</div>

</body>
</html>
EN

回答 1

Stack Overflow用户

发布于 2018-07-27 05:59:59

这里有几个问题

  1. 您正在将$error设置为布尔值,尝试使用布尔值定义数组键不起作用,它将继续只是布尔值编辑如果在将数组键=>值分配给它之前,$error的值求值为false,它将转换为数组,但如果它的值为true,它将导致Warning: Cannot use a scalar value as an array并继续成为布尔值
  2. 您永远不会检查$error以停止脚本执行和更新数据库
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51547964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档