使用多个提交和搜索处理表单中复杂场景的最佳方法

时间:2011-11-23 15:00:48

标签: php forms

我想问一下如何以及处理这种情况的最佳方法是什么,对我来说太复杂了。也许是高度赞赏的例子。

我想要构建的页面应具有此功能。

  1. 可以通过http://server/view.php?id=1000
  2. 在浏览器中打开
  3. 如果id = 1000不存在,则发布不存在的消息并将其丢给另一页
  4. 如果确实存在id = 1000,则打开要查看的页面
  5. 该页面有一个带有文本框的表单,我可以在其中添加有关记录的信息
  6. 表单提交给自己并将数据处理到DB。
  7. 我如何将所有这些功能结合起来?我真的很感激,如果一个示例代码段特别是在表单上提交,检查id是否存在感谢很多。

1 个答案:

答案 0 :(得分:2)

以下非常基本的例子。您可以填写详细信息

$id = intval($_GET['id']);  // assume it's int and force to be int

// do database call
// 1. init connection, set db, etc
mysql_query("select something from something where id = ".$id);

// check for results
if (! results exist) http_redirect('your error page');

// Now check if any posted variables exist
if (isset($_POST['field1']) && isset($_POST['field=2']) .... 
{
   1. validate your data
   2. insert to the database
   3. http_redirect to success page
}

// if you're getting here, that means id is valid and data has not been posted
 show your form
 display whatever HTML you need with the data loaded for id above
相关问题