我只想添加一名员工,之后员工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>
最佳答案
如果您想拥有像 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/