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);
}
?>