fetch next index in MySQL table
All Error Headling of this code
-- can not enter entered id in the table
-- only can enter next id according to the following table in project
university project
Code :
function/functions.php
<?php
use FTP\Connection;
include_once("config.php");
function add_user($id, $email, $usr_name){
$con = Connection();
$get_next_id = strval($_SESSION['nextId']);
$check_sql = "SELECT * FROM user_tbl WHERE id = '$id' || email = '$email'";
$check_sql_result = mysqli_query($con, $check_sql);
$check_sql_nor = mysqli_num_rows($check_sql_result);
if($check_sql_nor > 0){
return "<center> <div class='alert alert-danger col-10' role='alert'>User Already Exists..!</div> </center>";
}else{
if($id == $get_next_id){
$insert_user = "INSERT INTO user_tbl(id,email,usr_name)VALUES('$id','$email','$usr_name')";
$insert_user_result = mysqli_query($con, $insert_user);
header("location:index.php");
}
else{
return "<center> <div class='alert alert-danger col-10' role='alert'>Your entered id is ".$id."...! Enter ".$get_next_id." as Index Number</div> </center>";
}
}
}
function all_user(){
$con = Connection();
$all_users = "SELECT * FROM user_tbl";
$all_user_result = mysqli_query($con, $all_users);
while($user_row = mysqli_fetch_assoc($all_user_result)){
echo"<tr>
<td>".$user_row['id']."</td>
<td>".$user_row['email']."</td>
<td>".$user_row['usr_name']."</td>
</tr>";
}
}
function next_id(){
$con = Connection();
$get_last_index = "SELECT id FROM user_tbl ORDER BY id DESC LIMIT 1";
$get_last_index_result = mysqli_query($con, $get_last_index);
$last_index = mysqli_fetch_assoc($get_last_index_result);
$next_index = $last_index['id'] + 1;
echo $next_index;
$_SESSION['nextId'] = $next_index;
}
?>
Fetch Next Index....
function next_id(){
$con = Connection();
$get_last_index = "SELECT id FROM user_tbl ORDER BY id DESC LIMIT 1";
$get_last_index_result = mysqli_query($con, $get_last_index);
$last_index = mysqli_fetch_assoc($get_last_index_result);
$next_index = $last_index['id'] + 1;
echo $next_index;
$_SESSION['nextId'] = $next_index;
}
According to above part of code,
I fetch the last index using following lines
$get_last_index = "SELECT id FROM user_tbl ORDER BY id DESC LIMIT 1";
$get_last_index_result = mysqli_query($con, $get_last_index);
$last_index = mysqli_fetch_assoc($get_last_index_result);
and then I want to get next index, so that
I add 1 for the last index like following
$next_index = $last_index['id'] + 1;
echo $next_index;
and last line is optical,
$_SESSION['nextId'] = $next_index;
This is Optical
because I want to check user input id is equal to last index+1 in table the next id, so then I get that last id and
check it in
user_add() function
function add_user($id, $email, $usr_name){
$con = Connection();
$get_next_id = strval($_SESSION['nextId']);
$check_sql = "SELECT * FROM user_tbl WHERE id = '$id' || email = '$email'";
$check_sql_result = mysqli_query($con, $check_sql);
$check_sql_nor = mysqli_num_rows($check_sql_result);
if($check_sql_nor > 0){
return "<center> <div class='alert alert-danger col-10' role='alert'>User Already Exists..!</div> </center>";
}else{
if($id == $get_next_id){
$insert_user = "INSERT INTO user_tbl(id,email,usr_name)VALUES('$id','$email','$usr_name')";
$insert_user_result = mysqli_query($con, $insert_user);
header("location:index.php");
}
else{
return "<center> <div class='alert alert-danger col-10' role='alert'>Your entered id is ".$id."...! Enter ".$get_next_id." as Index Number</div> </center>";
}
}
}
converting Session to String like following
$get_next_id = strval($_SESSION['nextId']);
then check it,
if($id == $get_next_id){
$insert_user = "INSERT INTO user_tbl(id,email,usr_name)VALUES('$id','$email','$usr_name')";
$insert_user_result = mysqli_query($con, $insert_user);
header("location:index.php");
}
else{
return "<center> <div class='alert alert-danger col-10' role='alert'>Your entered id is ".$id."...! Enter ".$get_next_id." as Index Number</div> </center>";
}
in here $id is user input id
and $get_next_id is the next id according to the table
DEVELOPER : JEHANKANDY
THANK YOU