php - 使用php在一页上处理多个表单

标签 php html mysql

我在一个页面上有多个表单,我正在另一个名为 editmenusprocess.php 的页面中处理这些表单。

当我尝试提交个人表单时,信息将被放入数据库中。

但是,所有其他表单也都以空值提交。

我尝试过使用

if ($_POST['action'] == 'starter') 

作为解决方法。

我认为我没有正确实现它。

我在一页上有几个这样的表格。

<?php
  if ($_POST['action'] == 'starter') {
  } 
  else if($_POST['action'] == 'main' ){
  }
  else if($_POST['action'] == 'desert' ){    
  }
  else if($_POST['action'] == 'menus' ){
  }
?>

<div class='container-fluid bg-color' style='margin-bottom:20px'>
  <div class='row justify-content-center '>
    <div class='col-md-4'>
      <form action='editmenusprocess.php?action=starter' method='POST'>
        <legend>
          <h3 style='margin-top: 20px;'>Add Starter</h3>
        </legend>
        <div class='form-group '>
          <label for='exampleInputEmail1'>Food:</label>
          <input type='text' class='form-control form-control-lg' name='msfood' placeholder='Title'>
        </div>
        <div class='form-group '>
          <label for='exampleInputEmail1'>Description:</label>
          <input type='text' class='form-control form-control-lg' name='msdescript' placeholder='Title'>
        </div>
        <div class='form-group'>
          <label for='exampleInputEmail1'>Price:</label>
          <input type='text' class='form-control form-control-lg' name='msprice' placeholder='Info'>
        </div>
        <button type='submit' class='btn btn-primary bt'>Submit</button>
      </form>
    </div>

此代码位于流程页面上。

$msid = mysqli_real_escape_string($link, $_POST["msid"]); 
$msfood = mysqli_real_escape_string($link, $_POST["msfood"]);
$msdescript = mysqli_real_escape_string($link, $_POST["msdescript"]);
$msprice = mysqli_real_escape_string($link, $_POST["msprice"]);

$msinsertquery = "INSERT INTO 2043menustarter (id, food, descript,price) VALUES (null, '$msfood','$msdescript','$msprice')";
$msresult = mysqli_query($link, $msinsertquery) or die (mysqli_error($link));

我使用 ($_POST['action'] == 'starter') 方法在这个问题上取得了进展,表单现在将数据插入到相应的表中,但仍然弹出一些警告提交时向上。

这是编辑/表单页面的代码:

   <form action ='editmenusprocess.php' method = 'POST'>

      <legend><h3 style = 'margin-top: 20px;'>Add Starter</h3></legend>
      <div class='form-group '>
        <label for='fd1'>Food:</label>
        <input type='text' class='form-control form-control-lg' name='msfood'  id="fd1">
         <input type="hidden" name="action" value="starter">
      </div>

         <div class='form-group '>
        <label for='fd2'>Description:</label>
        <input type='text' class='form-control form-control-lg' name='msdescript'  id="fd2">
    <input type="hidden" name="action" value="starter">
      </div>

      <div class='form-group'>
        <label for='fd3'>Price:</label>
        <input type='text' class='form-control form-control-lg'  name = 'msprice' id=fd3>
         <input type="hidden" name="action" value="starter">
      </div>
          <button type='submit' class='btn btn-primary bt'>Submit</button>
    </form>
  </div>

          <div class= 'col-md-4'>
   <form action ='editmenusprocess.php' method = 'POST'>
      <legend><h3 style = 'margin-top: 20px;'>Add Main</h3></legend>
      <div class='form-group '>
        <label for='st1'>Food:</label>
        <input type='text' class='form-control form-control-lg' name='mmfood' id="st1">
         <input type="hidden" name="action" value="main">
      </div>
         <div class='form-group '>
        <label for='st2'>Description:</label>
        <input type='text' class='form-control form-control-lg' name='mmdescript'  id='st2'>
         <input type="hidden" name="action" value="main">
      </div>
      <div class='form-group'>
        <label for='st3'>Price:</label>
        <input type='text' class='form-control form-control-lg'  name = 'mmprice' id='st3'>
         <input type="hidden" name="action" value="main">
      </div>
      <button type='submit' class='btn btn-primary bt'>Submit</button>
    </form>

现在这是流程页面上的代码:

$action = (!empty($_POST["action"]))?$_POST["action"]:null;

$msfood = mysqli_real_escape_string($link, $_POST["msfood"]);
$msdescript = mysqli_real_escape_string($link, $_POST["msdescript"]);
$msprice = mysqli_real_escape_string($link, $_POST["msprice"]);


$mmfood = mysqli_real_escape_string($link, $_POST["mmfood"]);
$mmdescript = mysqli_real_escape_string($link, $_POST["mmdescript"]);
$mmprice = mysqli_real_escape_string($link, $_POST["mmprice"]);

if($action == 'starter'){

    $stmt = $link->prepare("INSERT INTO 2043menustarter (id,food, descript, price) VALUES (null, '$msfood','$msdescript','$msprice')");
    $stmt->bind_param('sss',$_POST["msfood"], $_POST["msdescript"], $_POST["msprice"]);
    $stmt->execute();
    $stmt->close();

}else if($action == 'main'){
    $stmt = $link->prepare("INSERT INTO 2043menumain (id,food, descript, price) VALUES (null, '$mmfood','$mmdescript','$mmprice')");
    $stmt->bind_param('sss',$_POST["mmfood"], $_POST["mmdescript"], $_POST["mmprice"]);
    $stmt->execute();
    $stmt->close();
}

最佳答案

如果您希望将操作读取为POST,请尝试将其放入隐藏输入中。您的其中一个表单如下所示:

<form action="editmenusprocess.php" method="POST">
    <legend><h3 style="margin-top: 20px;">Add Starter</h3></legend>
    <div class="form-group">
        <label for="exampleInputEmail1">Food:</label>
        <input type="text" class="form-control form-control-lg" name="msfood" placeholder="Title">
        <input type="hidden" name="action" value="starter">
    </div>
    <!-- THE REST OF THE INPUT FIELDS BELOW -->

对其他表单执行其余操作。

然后,在您的 editmenusprocess.php 中:

$action = (!empty($_POST["action"]))?$_POST["action"]:null;

if($action == 'starter'){

    $stmt = $link->prepare("INSERT INTO 2043menustarter (food, descript, price) VALUES (?, ?, ?)");
    $stmt->bind_param("sss", $_POST["msfood"], $_POST["msdescript"], $_POST["msprice"]);
    $stmt->execute();
    $stmt->close();

}
/*** THEN THE REST OF YOUR OTHER CONDITIONS BELOW ***/

在您帖子的评论部分,人们一直建议您至少使用准备好的声明。您可以阅读更多相关信息here .

关于php - 使用php在一页上处理多个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47782256/

相关文章:

返回 NULL 的 PHP MySQL 查询

html - 如何使用 LAMP 堆栈存储和访问博客内的图像?

php - 本地 xampp wordpress 安装 - 找不到 wp-blog-header.php

php - 这是 Mysqli 中的错误 - 部分结果

php - 在 php 中显示 group_concat 结果

PHP:使用另一个未排序的对象数组更新对象数组值

html - 为什么括号 "()"在 CSS 中没有下划线?

jquery - 如何在动画运行时暂时禁用 JQuery 单击处理程序?

php - 单击菜单更新表数据

mysql - 进行更高效的 COUNT