这是用户在其中输入数据的表单。
<form action="addemployee.php" method="POST" id="addEmployeeToDataBase">
<input type="text" name="username" placeholder="UserName..." class="txtInput" required="" />
<input type="text" name="fullname" placeholder="Full Name..." class="txtInput" required="" />
<br />
<input type="email" name="email" placeholder="Email Address..." class="txtInput" required="" />
<input type="password" name="password" placeholder="Password..." class="txtInput" required="" />
<br />
<select name="dept" id="txtInput">
<option value="-1">Select Department</option>
<option value="Back-Office">Back-Office</option>
<option value="HR">HR</option>
<option value="Marketing">Marketing</option>
<option value="Sales">Sales</option>
<option value="Technical">Technical</option>
</select>
<br />
<input type="text" name="address" placeholder="Address..." class="txtInput" required=""/>
<input type="text" name="city" placeholder="City..." class="txtInput" required="" />
<br />
<input type="text" name="pincode" placeholder="Pin/Postal/Zip Code..." class="txtInput" required="" />
<input type="text" name="country" placeholder="Country..." class="txtInput" required="" />
<br />
<p style="font-size: 17px;" />Date of Joining:<input type="date" name="joiningdate" placeholder="Joining Date" class="txtInput" required="" />
<br />
<input type="submit" name="submit" id="submit" value="Add Employee"/>
现在我想使用sql语句,输入的数据将同时发送到不同的表。
Table employee (emp_id, username, email, password, dept, address, city, pincode, country and joiningdate).
Table dept (dept_id, dept_name, dept_head, and emp_id).
此表单基本上插入到employee表中。
现在“问题”是我想分别将单一形式的数据插入到employee和dept表中,其中来自表单的dept的输入数据也应该插入到dept表的dept_name列中,并且还应该更新带有输入数据的dept表的emp_id。
请指导我。
答案 0 :(得分:2)
$query1= "INSERT INTO employee ( username, email,...)
VALUES ('".$_POST["username"]."', ...)";
if($result1 = mysql_query($query1))
{
$emp_id = mysql_insert_id();
$query2= "INSERT INTO dept ( emp_id, dept_name, ...)
VALUES ('".$emp_id."', '".$_POST["dept_name"]."',...)";
if($result2 = mysql_query($query2))
{
//success msg
}
}
答案 1 :(得分:1)
首先使用mysql函数last_insert_id
将数据插入到employee表中,您可以获取emp_id值并进入department表。
假设,emp_id是自动递增的值。
答案 2 :(得分:0)
这可能会有所帮助(我正在使用Ubuntu 12.04和LAMP,它对我有用)
$sql1 = "INSERT INTO users_account_details (uname,email,pass1,pass2)
VALUES('$_POST[uname]','$_POST[email]','$_POST[pass1]',DES_ENCRYPT('$_POST[pass2]'))";
$sql2 = "INSERT INTO users_personal_details (name,gender,dob,address,mobile)
VALUES('$_POST[name]','$_POST[gender]','$dob','$_POST[address]','$_POST[mobile]')";
if (!mysqli_query($con,$sql1))
{
die('Error: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql2))
{
die('Error: ' . mysqli_error($con));
}