How to generate auto number in php?
$servername="localhost"; $username="root"; $password =""; $dbname="emppp"; $conn=mysqli_connect($servername,$username, $password,$dbname); ?>
$query2="select * from student order by id desc limit 1"; $result2 =mysqli_query($conn,$query2); $row=mysqli_fetch_array($result2); $last_id= $row['id']; if($last_id=="") { $customer_ID ="CUS1"; } else { $customer_ID= substr($last_id,3); $customer_ID=intval($customer_ID); $customer_ID ="CUS".($customer_ID+1); } ?> $conn =mysqli_connect($servername,$username,$password,$dbname); if($_SERVER["REQUEST_METHOD"]== "POST"){ $id=$_POST["id"]; $stname= $_POST["stname"]; $course=$_POST["course"]; if (!$conn) { die("Connection failed: ".mysqli_connect_error()); } $sql="INSERT INTO student (id,stname,course )VALUES ('$id','$stname','$course')"; if(mysqli_query($conn, $sql)){ echo"New record created successfully"; }else{ echo
"Error: ".$sql." } mysqli_close($conn); } ?> <html> <head> <link rel="stylesheet"href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"crossorigin="anonymous"> </head> <body> <div class="container"> <div class="row"> <div class="col-sm-4"> <form action="echo ($_SERVER["PHP_SELF"]);?>"method="post"> <div align="left"> <h3>Autono Generating inphp</h3> </div> <div align="left"> <label>Employee No</label> <input type="text"class="form-control"name="id" id="id"style="color: red"value="echo$customer_ID;?>"readonly> </div> <div align="left"> <label>Employee Name</label> <input type="text"class="form-control"name="stname"id="stname"> </div> <div align="left"> <label>Employee Salary</label> <input type="text" class="form-control"name="course"id="course"> </div> </br> <div align="left"> <input type="submit"value="ADD"> </div> </form> </div> </div> </div> </body> </html> How to auto generate id in PHP MySQL?MySQL has the AUTO_INCREMENT keyword to perform auto-increment. The starting value for AUTO_INCREMENT is 1, which is the default. It will get increment by 1 for each new record. To get the next auto increment id in MySQL, we can use the function last_insert_id() from MySQL or auto_increment with SELECT.
How to auto generate unique id in MySQL?The MySQL UUID() function is used to return a Universal Unique Identifier (UUID) generated according to RFC 4122, “A Universally Unique IDentifier (UUID) URN Namespace”. A key point to note about UUIDs is that they are designed such that they are globally unique in space and time.
|