Select Max ID (row) from database using PHP

Some time we need to select just one max row from data base so that we increment in it and use for the next id so here is the simple solution of it in php


<?php 

include 'connect.php';

$id=0;

$sql1 = "select max(user_id) from register ;";
$result=$conn->query($sql1);
if ($result->num_rows > 0) 
{
            while($row = $result->fetch_assoc())
            { 
            $id=$row["max(user_id)"]+1;
            }
}
 
$sql = "INSERT INTO register (user_id) VALUES (".$id.");";

if ($conn->query($sql) === TRUE) {
    header ("location : login.html");   }
else{
Header("location: register.html?masg=some thing must be Wrong");
       }
?>



No comments

Powered by Blogger.