0

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);
			
	}
	
?>
3
  • 1
    use if(){.....}elseif(){...}else{..final..} Commented Sep 24, 2014 at 8:36
  • "I've tried bunch of things" we have no idea what those things are hence it is impossible to help. Also imho what you are trying to do will cause a pretty bad user experience, because the user will only know 1 fields is not filled in correctly and onyl submit he finds out there are other fields invalid. Commented Sep 24, 2014 at 8:39
  • You can use both PHP and Javascript Validation. Please check this, [Php/javascript email form and validation][1] [1]: stackoverflow.com/questions/19680406/… Commented Sep 24, 2014 at 8:58

1 Answer 1

0

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
 }
Sign up to request clarification or add additional context in comments.

2 Comments

You are a star! It worked, Thanks a lot, you have no idea how much I can enjoy my holiday tomorrow !!!
@ys1458 If this answer was helpfull for your question, plz mark it as its answer.. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.