2016-09-13 109 views
0

我只想添加一个员工,之后员工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> 
+0

将这些查询添加到表中EmpID INT NOT NULL AUTO_INCREMENT PRIMARY KEY – Prasad

+0

问题的一部分是,如果将其设置为自动增量,则在数据库中实际创建行之前,您不会知道密钥是什么。如果你只是希望它成为表的主键,用户永远不需要看到它。请注意,ID实际上不是一个数字(即使它是一串数字),所以您通常需要某种主角。 –

+0

谢谢你的回答。我真的很感激,但你能编辑我的代码吗?想象它?先谢谢你。 :) –

回答

0

禁用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

我可以放在哪里? :) –

+0

在将数据插入数据库之前放置代码 – Henry

+0

您可以向我展示它吗?在我的代码? –

0

广场AUTO_INCREMENT属性emp_id列在您的数据库。从您的用户界面中删除该EMP ID字段。

AUTO_INCREMENT属性可用于为新行生成唯一标识。手段,AUTO_INCREMENT自动每个INSERT query

Reference

+0

谢谢你的帮助。 :) –

+0

如果有任何帖子是有用的,你应该投票并选择该答案。这将有助于其他成员。 – AkshayP

+0

ohh。 OK2。哈哈。 :)我仍然是新的,顺便说一句,谢谢。 –

0

插入新的递增ID为那场你可以修改你的数据库中包括的EmpID AUTO_INCREMENT

您可以通过执行<input type="text" name="emp" placeholder="Emp ID" required disabled />

+0

感谢您的帮助Matu。 :) –

0

你可以隐藏Emp ID代码并将input type="text"更改为input type="hidden"并删除required在您的窗体中。 现在只需在表格中使用AUTO_INCREMENT代替EmpId