• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

自动增加员工ID

php 来源:Francis Vargas 3次浏览

我只想添加一个员工,之后员工ID也会增加。也是文本框必须被禁止自动增加员工ID

enter image description here

而且,这里是我的代码。我想要的只是在添加新员工时自动增加员工ID。我希望每个人都会帮助我。先谢谢你。 :)

<center> 
    <form class="contact_form" action="#" method="post"> 
      <h2>Register New Employee</h2> 
      <br/> 
      <table> 
       <tr> 
      <td><label for="emp">Emp ID:</label></td> 
      <td><input type="text" name="emp" placeholder="Emp ID" required /></td> 
     </tr> 
       <tr> 
      <td><label for="name">Name:</label></td> 
      <td><input type="text" name="fname" placeholder="First Name" required /> 
        <input type="text" name="lname" placeholder="Last Name" required /></td> 
     </tr> 
     <tr> 
      <td><label for="address">Address:</label></td> 
      <td><input type="text" name="address" placeholder="Full Address" required /></td> 
     </tr> 
     <tr> 
      <td><label for="contact">Contact:</label></td> 
      <td><input type="text" name="contact" placeholder="Contact Number" required /></td> 
     </tr> 
       <tr> 
        <td><label for="type">Type:</label></td> 
        <td><select name="type" id="type"> 
         <option>Type of Employee</option> 
         <option>Contractual</option> 
         <option>Regular</option> 
        </select> 
        </td> 
       </tr> 
       <tr> 
      <td><label for="salary">Salary:</label></td> 
      <td><input type="text" name="salary" placeholder="Emp Salary" required /></td> 
     </tr> 
     </table> 
      <br/> 
      <button class="submit" name="submit" type="submit" onclick="message()">Submit</button> 
      <button class="reset" name="reset" type="reset">Clear</button> 
     </form> 
     <?php 
     if (isset($_POST['submit'])) { 
      include 'alqdb.php'; 
       $emp=$_POST['emp']; 
       $fname= $_POST['fname']; 
       $lname=$_POST['lname']; 
       $address=$_POST['address']; 
       $contact=$_POST['contact']; 
       $type=$_POST['type']; 
       $salary=$_POST['salary']; 
      mysqli_query($con, "INSERT INTO employee (EmpID,EmpFName,EmpLName,EmpAddress,ContactNumber,TypeofEmployee,Salary) 
      VALUES ('$emp','$fname','$lname','$address','$contact','$type','$salary')"); 
     } 
     ?> 
    </center> 
</body> 
<script language="javascript"> 
    function message() { 
     alert("Successfully added!"); 
    } 
</script> 


===========解决方案如下:

禁用input如果你想有一个的EmpID像EMP001

这个代码将工作:

公共职能autoincemp()

{ 
    global $value2; 
    $query = "SELECT empid from tbemployee order by empid desc LIMIT 1"; 
    $stmt = $this->db->prepare($query); 
    $stmt->execute(); 

    if ($stmt->rowCount()>0) 
    { 
    $row=$stmt->fetch(PDO::FETCH_ASSOC); 

     $value2 =$row['empid']; 
     $value2 =substr($value2,3,5); 
     $value2 = (int)$value2 + 1; 
     $value2 = "EMP". sprintf('%04s',$value2); 

$value = $value2; 
return $value; 
    } 
    else { 
     $value2 = "EMP0001";  
    $value = $value2; 
    return $value; 
    } 
    } 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)