php - 自动增加员工ID

标签 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>

最佳答案

如果您想拥有像 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;
    }
}

关于php - 自动增加员工ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39462985/

相关文章:

HTML 表单 POST 跨域

mysql - 将自动增量大小设置为 200001 并偏移 10000

php - 在 HTML 表中显示 10 个最近的数据库条目

Mysql连接两个表并避免重复

php 回显 javascript 调用

javascript - 从表行获取 HTML 数据点击进入 javascript 警报

php - 表中没有数据

javascript - 如何在 html 中创建模板或占位符并将它们动态添加到正文中?

php - 如何根据值显示多个表格

javascript - php 表单 html 按钮