duanping5306 2014-03-26 02:44
浏览 53
已采纳

如何在PHP中为MySQL插入查询函数定位单个表单?

I have three different forms on a single page. Each form's data will be entered into a different table. For the first one I created I'm using this if statement to insert the data into the table:

if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
$band_name = $_POST['bandname'];
$band_website = $_POST['website'];
$band_descrip = $_POST['description'];

query(
    "INSERT INTO bands(bandname, website, description) VALUES(:bandname, :website, :description)",
    array('bandname' => $band_name, 'website' => $band_website, 'description' => $band_descrip),
    $conn
    );
} else {
$status = "Fill out the form";
}

I am assuming I need to create three different versions of the if statement, one for each form. How can I specify which form was submitted?

  • 写回答

1条回答 默认 最新

  • douxu3732 2014-03-26 02:49
    关注

    Since when you submit a form, only the data in the form tags are submitted you are able to check what form by using something to identity the forms. I usually use the submit buttons name to check for the form. You can use any $_POST variable as long as you only use it for one form.

    <form method="post">
       <input type="text" name="form1_text" />
       <input type="submit" name="form1_submit" value="Submit" />
    </form>
    
    <form method="post">
       <input type="text" name="form2_text" />
       <input type="submit" name="form2_submit" value="Submit" />
    </form>
    
    <?php
       if(isset($_POST['form2_submit'])) {
          // Handle Form 2
       }else if(isset($_POST['form1_submit'])) {
          // Handle Form 1
       }
    ?>
    

    For more information on how web browsers should handle forms, you can check the HTML documentation on W3.org - Forms in HTML documents. This explains what data is sent to the server and how that data is selected / determined.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog