PHP多个表单,一个函数
这个逻辑把我弄糊涂了。我想写一个函数来处理多个表单。我有八份订单,在不同的页面上。它们都使用相同的数据库。该函数将需要处理插入函数或更新函数 我在被难倒之前写的是:PHP多个表单,一个函数,php,forms,logic,Php,Forms,Logic,这个逻辑把我弄糊涂了。我想写一个函数来处理多个表单。我有八份订单,在不同的页面上。它们都使用相同的数据库。该函数将需要处理插入函数或更新函数 我在被难倒之前写的是: // I might want to move this IF further down in the logic // I wasn't sure what to put here in it's place. if(isset($_POST['submitInsert'])) { // setting up a loop to
// I might want to move this IF further down in the logic
// I wasn't sure what to put here in it's place.
if(isset($_POST['submitInsert']))
{
// setting up a loop to go through all the POST values
foreach($_POST as $name => $value)
{
// make sure POST values are set before wasting
// any time doing stuff with them
if($value!="")
{
// checking if the POST value is an array because
// an array would have to be handled differently
if(!is_array($value))
{
// Just printing info for now, Thinking maybe I should
// do an IF here to check for insert/update
echo $name . " --- " . $value . "<br>";
}
else
{
// for an array, I'll need another foreach statement
echo "$name"." --- ";
print_r($value);
echo "<br/>";
}
}
}
}
更多信息
我计划通过将提交按钮命名为“submitInsert”或“submitUpdate”,在插入/更新之间切换。在一些数据验证之后,检查插入/更新。在决定这是一种方式之后,我意识到我不知道如何启动该功能
表单元素名称与db字段名称匹配,因此SQL查询应该不难
有更好的方法吗?因此,如果我理解正确,您计划使用相同的功能来处理插入和更新?这有什么原因吗?我唯一想知道的是:如果用户试图提交一个新订单,并且订单号已经存在于数据库中,这个函数只会更新订单。它不会通知用户。相反,如果用户试图更新输入新订单的订单,那么最终将创建一个新订单。有没有办法捕捉到这一点?订单号是唯一的,数据库应该生成它而不是用户,如果用户有订单号,那么他可以更新,而且你不允许用户输入他的订单号,理想情况下,他会从预定义列表中选择。我的错误是将订单号等同于$id。我的系统不会创建订单号,另一个系统会创建。订单号用其中一台红色激光扫描仪扫描。因此,从逻辑上讲,用户正在创建订单号,而我的系统只是记录它。订单号仍然是唯一的,但在数据库中不是自动递增的ID。您是否没有访问生成订单号的数据库的权限,如果您有访问权限,则可以简单地进行交叉引用?最简单的方法是在生成订单号时将其插入表中,之后的一切都是更新。我没有访问其他数据库的权限。
if(isset($_POST['submit']))
{
//could use for loop but you know the fields you will be using for your sql.
$id = $_POST['id'];
$var1 = $_POST['var1'];
// Do a select see if it exists if it does use update else use insert.
$query = "SELECT * from table where ID = $id";
if($rowCount > 0)
{
$query = "Update table set var=$var1 where id = $id";
}
else
{
$query = "Insert into table (var1) VALUES ($var1)";
}
}