【问题标题】:Auto increment employee ID自动增加员工 ID
【发布时间】:2020-11-25 21:30:14
【问题描述】:

我只想添加一个员工,然后员工 ID 也会增加。还必须禁用该文本框

这也是我的代码。我想要的只是在添加新员工时自动增加我的员工 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>

【问题讨论】:

  • 在表 EmpID INT NOT NULL AUTO_INCREMENT PRIMARY KEY 中添加这些查询
  • 部分问题是,如果将其设置为自动增量,则在您实际在数据库中创建行之前,您不会知道键是什么。如果您只是希望它成为表的主键,则用户永远不需要看到它。请注意,ID 实际上并不是一个数字(即使它是一串数字),因此您通常需要某种前导字符。
  • 感谢您的回答。我真的很感激,但你能编辑我的代码吗?把它形象化?先感谢您。 :)

标签: php html mysql sql


【解决方案1】:

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

【讨论】:

  • 我可以把它放在哪里? :)
【解决方案2】:

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

你可以通过&lt;input type="text" name="emp" placeholder="Emp ID" required disabled /&gt;禁用input

【讨论】:

    【解决方案3】:

    AUTO_INCREMENT 属性放在数据库中的emp_id 列中。从您的 UI 中删除该 EMP ID 字段。

    AUTO_INCREMENT 属性可用于为新行生成唯一标识。意味着,AUTO_INCREMENT 自动为每个 INSERT query 上的该字段插入新的递增 id

    Reference

    【讨论】:

    • 如果任何帖子有用,您应该投票并选择该答案。这将对其他成员有所帮助。
    • 哦。好的2。哈哈。 :) 我还是新手,顺便说一句谢谢。
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

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

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

      希望这对某人有所帮助。

      Reference

      【讨论】:

        【解决方案6】:
        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)
        

        【讨论】:

        • 答案应该与 MySQL 有关,因为问题提到了这个数据库。
        猜你喜欢
        • 2015-03-19
        • 2016-11-19
        • 2011-04-26
        • 2010-12-09
        • 1970-01-01
        • 1970-01-01
        • 2021-03-25
        • 2018-12-06
        • 1970-01-01
        相关资源
        最近更新 更多