我在一个页面上有多个表单,我正在另一个名为 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/