自动增加员工ID

时间:2016-09-13 05:50:51

标签: php html mysql sql

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

6 个答案:

答案 0 :(得分:0)

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

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

上的该字段插入新增加的ID

<强> Reference

答案 1 :(得分:0)

您可以修改数据库以在EmpID中包含AUTO_INCREMENT

您可以通过input

停用<input type="text" name="emp" placeholder="Emp ID" required disabled />

答案 2 :(得分:0)

如果您想拥有像EMP001这样的EmpID

此代码将起作用:

public function 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;
    }
}

答案 3 :(得分:0)

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

答案 4 :(得分:0)

已经很晚了,但你可以这样做,

$id = 1; //Your last record Id + 1
str_pad($id, 3, "0", STR_PAD_LEFT);

希望这有助于某人。

Reference

答案 5 :(得分:-1)

REATE TABLE dbo.tblUsers
  (ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
   UserID AS 'UID' + RIGHT('00000000' + CAST(ID AS VARCHAR(8)), 8) PERSISTED,
   .... your other columns here....
  )

现在,每次将行插入tblUsers时都未指定ID或UserID的值:

INSERT INTO dbo.tblUsersCol1, Col2, ..., ColN)
VALUES (Val1, Val2, ....., ValN)