gpt4 book ai didi

php - 如何从选项获取输入,而我必须将数据输入到同一数据库的两个表中,基于选项数据将被插入

转载 作者:行者123 更新时间:2023-11-29 09:35:19 26 4
gpt4 key购买 nike

我在数据库中有两个表,我必须通过表单输入数据,但条件是 - 表单中有一个选择选项,并且作为我们必须插入数据的选项。如果选择选项 1,则数据应插入到表 x如果选择选项 2,则数据应插入到表 y

我尝试过使用 switch case 和 elseif但不能 找不到合适的条件。 html代码-

        <form method="post" class="register-form" id="register-form">
<div class="form-group">
<label for="name"><i class="zmdi zmdi-account material-icons-name"></i></label>
<input type="text" name="aname" id="name" placeholder="Your Name"/>
</div>

<div class="form-group">
<label for="email"><i class="zmdi zmdi-email"></i></label>
<input type="email" name="email" id="email" placeholder="Your Email"/>
</div>

<div class="form-group">
<label for="address"><i class="zmdi zmdi-home"></i></label>
<input type="text" name="address" id="address" placeholder="Your Address"/>
</div>
<div class="form-group">
<label for="user"><i class="zmdi zmdi-accounts-alt"></i></label>
<select id="person-type" name="logtype" required style="border-top: rgba(0,0,0,0.0);border-right: rgba(0,0,0,0.0);border-left: rgba(0,0,0,0.0); width: 100%;margin-left: 22px;">
<option disabled selected value="">Select your login type</option>
<option value="consumer" name="consumer">Consumer</option>
<option value="electrician" name="electrician">Electrician</option>
</select>
</div>

<div class="form-group">
<label for="pass"><i class="zmdi zmdi-lock"></i></label>
<input type="password" name="apass" id="password" pattern=".{6,}" required placeholder="Password " oninvalid="this.setCustomValidity('Password must be 6 digit long or more')" oninput="this.setCustomValidity('')" />
</div>
<div class="form-group">
<label for="re-pass"><i class="zmdi zmdi-lock-outline"></i></label>
<input type="password" name="re_pass" id="confirm_password"required="@" minlength="8" placeholder="Repeat your password"/>
</div>
<div class="form-group">
<input type="checkbox" name="agree-term" id="agree-term" class="agree-term" required/>
<label for="agree-term" class="label-agree-term"><span><span></span></span>I agree all statements in <a href="#" class="term-service">Terms of service</a></label>
</div>
<div class="form-group form-button">
<input type="submit" name="signup" id="signup" class="form-submit" value="Register"/>
</div>
</form>

php 代码-

if (isset($_POST['signup'])) 
{
if ($_POST['logtype'] == 'consumer')
{
$consumer=mysqli_query($edb,"insert into consumer(name,email,address,logtype,password) values('".$_POST['aname']."','".$_POST['email']."','".$_POST['address']."','".$_POST['logtype']."',md5.
('".$_POST['apass']."'))");
header("Location:loginonly.php");
}
elseif ( $_POST['logtype'] == 'electrician' )
{

$electrician=mysqli_query($edb,"insert into electrician(name,email,address,logtype,password) values('".$_POST['aname']."','".$_POST['email']."','".$_POST['address']."','".$_POST['logtype']."',md5.
('".$_POST['apass']."'))");
header("Location:loginonly.php");
}

没有错误,但数据未插入表中

最佳答案

您应该在查询执行中添加一些错误处理,以帮助查找发生的情况。

PHP 中的基本 mysqli 错误处理类似于:

<?php
if (!mysqli_query($edb,"YOUR QUERY HERE"))
{
echo("Error description: " . mysqli_error($con));
}
?>

关于php - 如何从选项获取输入,而我必须将数据输入到同一数据库的两个表中,基于选项数据将被插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57770788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com