PHP逐步验证用户输入数据

时间:2014-09-24 08:34:54

标签: php if-statement

我对PHP很新,因为现在我不得不用PHP编写小程序,而我的公司找到了合适的程序员。

我编写了一个使用表单数据(名称,电子邮件,手机号码等)的程序,然后检查输入的数据是否有效。我的代码工作正常,但他们不遵循任何程序(即它检查所有验证)。我希望它首先检查一个条件(如电子邮件)是否有效,然后只检查下一个条件(电话号码)。如果它无效,则返回一些错误消息&停止浏览其余的代码。

我尝试过很多东西,但没有一个有效,这里是示例代码

<?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 个答案:

答案 0 :(得分:0)

试试这个。我不确定你是否想要这个......

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

然后创建一个名为is_email()的函数并在那里编写验证码,如果是电子邮件格式则返回true,否则返回false

 is_email($email)
 {
     //validation code
 }