php - 单个 PHP 上的多种表单

标签 php html mysql

我正在尝试在 PHP 文件上创建两个表单。处理PHP代码位于同一页面上,但我不知道如何将表单1连接到代码1,将表单2连接到代码2。我知道这可能是一个非常基本的问题,但我不知道该怎么做它..谢谢!这是我的代码(我只发布重要部分,而不是连接等)

    <div class="persinfo">

<?php

$bday = $_POST['bday']; 
$sex = $_POST['sex'];  
$flighttime = $_POST['flighttime'];
$country = $_POST['country'];

$sqlupdate = "SELECT * FROM users WHERE username='$u' AND activated='1'";
$user_query = mysqli_query($db_conx, $sqlupdate);

mysqli_query($db_conx,"UPDATE users SET country='$country', bday='$bday', sex='$sex', flighttime='$flighttime' WHERE username='$u' ");

?>

    <form method="post" /> 

        <p><span>Country: </span> <input type="text" name="country" id="country" value="<?php echo $country; ?>"></p>
        <p><span>Gender: </span>
            <select name="sex" id="sex">
                <option>Male</option>
                <option>Female</option>
            </select></p>
        <p><span>Birthday: </span><input type="date" name="bday" id="bday" value="<?php echo $bday; ?>"></p>
        <p><span>Flight experience: </span>
            <select name="flighttime" id="flighttime">
                 <option>0-500</option>
                 <option>500-1000</option>
                 <option>1000-5000</option> 
                 <option>5000 +</option>    
            </select></p>

         <p><input  name="submit_profile" type="submit" value="Save"></p>

    </form>


    </div>

<!-- ===== EDIT LICENSES ======= -->

    <div class="persinfo">

<?php

$glider = $_POST['glider'];
$commercial=$_POST['Commercial'];
$seaplane=$_POST['seaplane'];
$fixedwing=$_POST['fixedwing'];
$helicopter=$_POST['helicopter'];
$balloon=$_POST['balloon'];
$paraglider=$_POST['paraglider'];
$ultralight=$_POST['ultralight'];

$sqlupdate = "SELECT * FROM users WHERE username='$u' AND activated='1'";
$user_query = mysqli_query($db_conx, $sqlupdate);

mysqli_query($db_conx,"UPDATE users SET glider='$glider', commercial='$commercial', seaplane='$seaplane', fixedwing='$fixedwing', helicopter='$helicopter', balloon='$balloon', ultralight='ultralight', paraglider='$paraglider'  WHERE username='$u' ");

?>

    <form method="post"/> 

        <p><span>Helicopter: </span><input type="checkbox" name="helicopter"  id="helicopter" value="1" <?php echo ($helicopter==1 ? 'checked' : '');?>></p>
        <p><span>Fixed Wing: </span><input type="checkbox" name="fixedwing"  id="fixedwing" value="1" <?php echo ($fixedwing==1 ? 'checked' : '');?>></p>
        <p><span>Commercial: </span><input type="checkbox" name="commercial"  id="commercial" value="1" <?php echo ($commercial==1 ? 'checked' : '');?>></p>
        <p><span>Seaplane: </span><input type="checkbox" name="seaplaner"  id="seaplane" value="1" <?php echo ($seaplane==1 ? 'checked' : '');?>></p>
        <p><span>Glider: </span><input type="checkbox" name="glider"  id="glider" value="1" <?php echo ($glider==1 ? 'checked' : '');?>></p>
        <p><span>Para: </span><input type="checkbox" name="paraglider"  id="paraglider" value="1" <?php echo ($paraglider==1 ? 'checked' : '');?>></p>
        <p><span>Balloon: </span><input type="checkbox" name="balloon"  id="balloon" value="1" <?php echo ($balloon==1 ? 'checked' : '');?>></p>
        <p><span>Ultralight: </span><input type="checkbox" name="ultralight"  id="ultralight" value="1" <?php echo ($ultralight==1 ? 'checked' : '');?>></p>

         <p><input  name="submit_lic" type="submit" style="margin: 0 auto; width:100px;" value="Save"></p>

    </form>

    </div>

最佳答案

通常的方法是使用一个隐藏的输入字段来指定每个表单的形式。例如:

<form id="Form1">
    <input type="text" name="formName" value="form1" hidden />
    <!-- add the rest of inputs for the form -->
</form>

<form id="Form2">
    <input type="text" name="formName" value="form2" hidden />
    <!-- add the rest of input for the form -->
</form>

然后在你的 PHP 中:

$formName = $_GET['formName'];

if($formName === 'form2'){
    // do something for form2
}else{
    // do something for form1
}

注意

我在您的代码中注意到的一点是,它相当容易受到称为“SQL 注入(inject)”的攻击类型的攻击。基本上,通过直接将 $_POST 中的数据放入 SQL 查询中,攻击者就有可能将恶意脚本插入到输入字段中。如果这最终要成为现场形式,则需要注意一些事情。

更多信息请点击:SQL Injections

关于php - 单个 PHP 上的多种表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30920952/

相关文章:

javascript - 如何确定静态 Google map 缩放级别

php - 使用 mysql_fetch_assoc 获取数组

html - VIM 中的 HTML 和 CSS 文件没有自动完成 (YouCompleteMe)

javascript - 单击放大 img,再次单击最小化

Mysql外键约束格式不正确?

php - 使用 MySQL 查询作为 PHP 脚本的标识,将 FCM 推送通知发送到 Android 应用程序中的特定设备

php - 验证一个字段 mysql laravel 5

javascript - 按下拉框中的选定月份过滤表格

mysql - 我们如何合并两个具有相同模式的数据库?

html - 调整旋转木马 Bootstrap 4