【发布时间】:2014-10-02 14:09:26
【问题描述】:
逻辑让我感到困惑。我想写一个函数来处理多种形式。我有八个订单表格,在不同的页面上。他们都使用相同的数据库。该函数将需要处理插入函数或更新函数。
我在被难住之前写的:
// 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查询应该不难。
有没有更好的方法来做到这一点?
【问题讨论】:
-
那么,如果我的理解正确,您打算使用相同的函数来处理插入和更新?这是有原因的吗?