douyu7210 2014-09-24 08:34
浏览 53
已采纳

PHP逐步验证用户输入数据

I am fairly new to PHP, for now I am having to write small programs in PHP while my company finds a suitable programmer.

I have written a program which uses form data (Name, E-mail, mobile number etc) and then checks if the entered data is valid. My code work just fine but they don't follow any procedure (i.e. it checks all the validation). I want it to first check one condition (like E-mail) if its valid, then only check the next condition (phone number). If its invalid, then return some error message & stop going through rest of the code.

I've tried bunch of things but none has worked, here's the sample code

<?php include("includes/functions.php"); ?>
<?php include("includes/dbconnection.php"); ?>
    
<?php
    //I've done this in main.php page but also had to do it in functions.php page too...
    $First_Name = $_POST['first_name'];
    $Last_Name = $_POST['last_name'];
    $Phone_Number = $_POST['phone_number'];
    $E_Mail = $_POST['email'];
    $User_ID = $_POST['user_id'];
?>   
    
<?php $user_id_validation = validate_user_id($User_ID); ?>
<?php $phone_number_validation = validate_phone_number(); ?>
<?php $all_fields_validation = validate_all_fields(); ?>

<?php 
    //This is dbconnection.php page
    define("DB_SERVER", "localhost");
    define("DB_USER", "root");
    define("DB_PASS", "some_password");
    define("DB_NAME", "some_db");

     
      $connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME); //This $connection has been used as global in function.php 
      
      if(mysqli_connect_errno()) {
        die("Database connection failed: " . 
             mysqli_connect_error() . 
             " (" . mysqli_connect_errno() . ")"
        );
      }
?>
<?php
    //This is functions.php
    function validate_user_id($User_ID) {
        global $connection;
        
        $sql = "SELECT * FROM users WHERE user_id = '$User_ID'";
        $result = mysqli_query($connection,$sql);
        $num_row = mysqli_num_rows($result);
        if ($num_row == 1) {
        $User_ID = $num_row['user_id'];
        echo "user verification passed! ";
        } else {
            echo "You are not authorized to perform this action! ";
        }
        return false;
    }
    
    function validate_phone_number() {
        $Phone_Number = $_POST['phone_number'];
        $check_phone_number = substr($phone_number, 0, -7); //Take first 3 digits of phone number to validate if its standard phone number
                
        if (strlen($Phone_Number) != 10) {
            echo "Invalid Phone Number";
        } elseif ($check_phone_number === "740") {
            echo "Phone Number is valid ";
        } else {
            echo "Invalid Phone Number, please check again! ";
        }
    }
        
    function validate_all_fields() {
        $First_Name = $_POST['first_name'];
        $Last_Name = $_POST['last_name'];
        $Phone_Number = $_POST['phone_number'];
        $E_Mail = $_POST['email'];
        $User_ID = $_POST['user_id'];
        
        if (!empty($E_Mail)
        && !empty($Phone_Number) 
        && !empty($First_Name)
        && !empty($Last_Name))
        
        ) {
            echo "All fields entered, you can proceed. ";
        } else {    
            echo "E_Mail, Phone Number, First Name and Last Name must be entered!"; 
        }
    }
    // Database Insertion
    function Insert_Into_Table () {
        global $connection;
        $First_Name = $_POST['first_name'];
        $Last_Name = $_POST['last_name'];
        $Phone_Number = $_POST['phone_number'];
        $E_Mail = $_POST['email'];
        $User_ID = $_POST['user_id'];
        
        $sql = "INSERT INTO some_tbl(User_ID, First_Name, Last_Name, Phone_Number, E_Mail)
                VALUES ('$User_ID', '$First_Name', '$Last_Name', '$Phone_Number', '$E_Mail')";
        
        $result = mysqli_query($connection, $sql);
            
    }
    
?>


  • 写回答

1条回答 默认 最新

  • doucongmishang2385 2014-09-24 09:06
    关注

    Try this. Im not sure whether you want this or not..

    if(!is_email($email))
    {
        echo 'Invalid email!!';
        exit();
    }
    elseif(!is_phone($phone)) 
    {
        echo 'Invalid Phone!!';
        exit();
    }
    else {
       //success code
    }
    

    Then create a function named is_email() and write the validation code there and return true if it is in email format otherwise return false

     is_email($email)
     {
         //validation code
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解