Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php在一个页面上处理多个表单_Php_Html_Mysql - Fatal编程技术网

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

使用php在一个页面上处理多个表单,php,html,mysql,Php,Html,Mysql,我在一个页面上有多个表单,我正在另一个名为editmenusprocess.php的页面上处理这些表单 当我试图提交一个单独的表单时,信息会被放在数据库中 但是,所有其他表单也使用空值提交 我试过使用 if ($_POST['action'] == 'starter') 作为一种工作方法 我认为我没有正确地实施它 我在一页中有几张这样的表格 <?php if ($_POST['action'] == 'starter') { } else if($_POST['actio

我在一个页面上有多个表单,我正在另一个名为
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>
我使用
($\u 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>

操作值编码在查询字符串中

PHP将查询字符串解析为
$\u GET
超全局。只有来自请求正文的数据将被解析到
$\u POST
超全局


使用
$\u GET['action']
或将数据从查询字符串移动到输入。

操作值编码在查询字符串中

PHP将查询字符串解析为
$\u GET
超全局。只有来自请求正文的数据将被解析到
$\u POST
超全局


使用
$\u GET['action']
或将数据从查询字符串移动到输入。

如果希望将
操作作为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 -->

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

如果您希望将
操作作为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 -->

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

如果希望将字段单独发送给其他脚本,则有两种选择

1.将输入字段放入它们自己的表单中(使用它们自己的提交按钮)
如果希望将字段单独发送到另一个脚本,则有2个选择

1.将输入字段放入它们自己的表单中(使用它们自己的提交按钮)

抱歉,在那里编辑了它以包含它警告:当使用
mysqli
时,您应该使用并将用户数据添加到您的查询中。不要使用手动转义和字符串插值或串联来完成此操作,因为这样会创建严重的错误。意外未扫描的数据是一个严重的风险。使用绑定参数不那么冗长,也更容易检查是否正确。在您的示例中,
操作
将是一个变量,而不是一个变量。@tadman我完全同意,但这是一个大学项目,奇怪的是,我们被告知要将
$\u POST
与servers@Chris-您确定没有将发出POST请求的指令与使用
$\u POST
超全局的指令混淆吗?(这听起来像是对showdev的响应,而不是对tadman的响应)。抱歉,在这里编辑了它,以包含它警告:当使用
mysqli
时,您应该使用并将用户数据添加到您的查询中。不要使用手动转义和字符串插值或串联来完成此操作,因为这样会创建严重的错误。意外未扫描的数据是一个严重的风险。使用绑定参数不那么冗长,也更容易检查是否正确。在您的示例中,
操作
将是一个变量,而不是一个变量。@tadman我完全同意,但这是一个大学项目,奇怪的是,我们被告知要将
$\u POST
与servers@Chris-您确定没有将发出POST请求的指令与使用
$\u POST
超全局的指令混淆吗?(这听起来像是对showdev的回应,而不是对tadman的回应)。谢谢,我们正在取得进展!提交“我的表单”时,仅向相应的数据库表插入数据。但是我得到了错误,我作为值传入的$variables的未定义索引和mysqli_stmt::bind_param():变量的数量与参数的数量不匹配。您必须确保参数的数量与变量的数量匹配。您当前的查询是什么?我更新了顶部的代码,我想可能是id,它在表中自动递增,所以我不希望用户传入值。我不确定是否可以传入(id,…)值(null,…),因为表单提交中没有设置id?@Chris因为
id
是自动递增的,您可以将其留空,就像我提供的插入查询一样。您不必将变量直接放入查询中,只需将其保留为
。它们将由下一行的prepared语句参数化。您也不需要手动转义POST数据。@Chris很乐意提供帮助。更新帖子的最后一个注意事项是,您只能在每个表单中放置一个隐藏的输入字段,而不能在文本字段之后放置每个隐藏的输入字段,并删除使用
\u real\u escape\u string()
的变量声明,因为您已经使用了
准备语句
,因此不需要这些变量。感谢您取得了进展!提交“我的表单”时,仅向相应的数据库表插入数据。但是我得到了错误,我作为值传入的$variables的未定义索引和mysqli_stmt::bind_param():变量的数量与参数的数量不匹配。您必须确保参数的数量与变量的数量匹配。您当前的查询是什么?我更新了顶部的代码,我想可能是id,它在表中自动递增,所以我不希望用户传入值。我不确定是否可以传入(id,…)值(null,…),因为表单提交中没有设置id?@Chris因为
id
是自动递增的,您可以将其留空,就像我提供的插入查询一样。和y
<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 -->
$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 ***/
<form method="post" action="somescript.php">
    <input type="text" name="input1"><button type="submit">save</button>
</form>
<form method="post" action="somescript.php">
    <input type="text" name="input1"><button type="submit">save</button>
</form>
$('form').submit(function(e){
    e.preventDefault();
    // clean your data
    var yourCleanedData = [];
    $('form input').each(functionn(idx, elm){
        if (elm.value){
            yourCleanedData[elm.name] = elm.value;
        }
    });
    $.ajax({
        url: 'somescript.php',
        type: 'POST',
        data: yourCleanedData,
    })
});