我创建了一个可以接受两种类型用户的表单。 select标签中提供了不同的用户类型。我需要根据用户的选择将数据插入两个单独的表中。使用我当前的代码,我无法将数据插入任何一个表。大多数表单输入在两个表中都很常见。我还需要所有输入字段中的'required'属性。
这是表格的图像:
这是HTML:
<form action="login.php" method="post">
<div class="field-wrap">
<label>Name<span class="req">*</span></label>
<input type="text" name="name" required autocomplete="off" />
</div>
<div class="field-wrap">
<label>Email Address<span class="req">*</span></label>
<input type="email" name="email" required autocomplete="off" />
</div>
<div class="field-wrap">
<label>Phone Number<span class="req">*</span></label>
<input type="tel" name="phone_no" required autocomplete="off" maxlength="10" />
</div>
<div class="field-wrap">
<label>Set A Password<span class="req">*</span></label>
<input type="password" name="password" required autocomplete="off" />
</div>
<!--For selecting the type of user-->
<div class="field-wrap combo">
<label>Account Type</label>
<select class="AccountType" id="account_type" name="account_type">
<option value="AT" selected disabled hidden>Choose Your Account Type</option>
<option value="User">User</option>
<option value="Agent">Agent</option>
</select>
</div>
<!--These are the options available only for agents-->
<div id="agent_options">
<div class="field-wrap">
<label>Agency Name<span class="req">*</span></label>
<input type="text" name="agency_name" <?php echo $required ?> autocomplete="off" />
</div>
<div class="field-wrap">
<label>Street Name<span class="req">*</span></label>
<input type="text" name="street_name" <?php echo $required ?> autocomplete="off" />
</div>
<div class="field-wrap">
<label>City<span class="req">*</span></label>
<input type="text" name="city" <?php echo $required ?> autocomplete="off" />
</div>
<div class="field-wrap">
<label>State<span class="req">*</span></label>
<input type="text" name="state" <?php echo $required ?> autocomplete="off" />
</div>
</div>
<button type="submit" class="button button-block" name="register">CREATE ACCOUNT</button>
</form>
这是我用过的PHP代码。
if(isset($_POST['register']))
{
if(isset($_POST['account_type']))
{
$account_type=$_POST['account_type'];
if($account_type=="Agent")
$required="required";
else
$required="";
}
if($account_type=="User")
{
$user_name=$_POST['name'];
$email=$_POST['email'];
$phone_no=$_POST['phone_no'];
$password=$_POST['password'];
$query="INSERT INTO user_details (user_name,email,phone_no,password) VALUES ('$user_name','$email','$phone_no','$password')";
mysqli_query($connection,$query);
}
else
{
$user_name=$_POST['name'];
$email=$_POST['email'];
$phone_no=$_POST['phone_no'];
$password=$_POST['password'];
$agency_name_name=$_POST['agency_name'];
$street_name=$_POST['street_name'];
$city=$_POST['city'];
$state=$_POST['state'];
$query="INSERT INTO agent_details (agent_name,email,phone_no,password,agency_name,street_name,city,state) VALUES ('$user_name','$email','$phone_no','$password','$agency_name','$street_name','$city','$state')";
mysqli_query($connection,$query);
}
}