如何将 mysql_insert_id() 传递给 $_POST 或表单操作?

发布于 2024-12-07 19:26:37 字数 866 浏览 1 评论 0原文

我有一个表单页面,其中提交 INSERT 或 UPDATE 查询,具体取决于 ID 的存在/不存在(当存在 ID 时,它用于检索记录并预填充表单)。无论哪种情况,处理都是在 form.php 中进行的,因此表单的操作就是其本身 (action="/form.php">)。我的问题是,当 form.php 重新加载提交后时,URL 的 ID 为空,因此页面进入“INSERT”模式,而不是“UPDATE”模式。解决这个问题的最佳实践方法是什么?

  1. 我应该向这个“if”添加什么运算符/条件...

    if (isset($_GET['ID']) && is_numeric($_GET['ID'])) {

... 包括提交后空 ID URL(即form.php?ID=)

或者,

  1. 如何将 `$newID = mysql_insert_id();1 传递给表单的操作? (我在这里尝试了多种变体,但没有成功)

    $newID = mysql_insert_id(); ...[剪断]...

我正在阅读有关隐藏输入和会话的信息但我还不清楚如何使用其中任何一个来解决这个问题。最后,由于重新加载表单页面并不是绝对必要的,因此我越来越倾向于将表单处理/数据库查询移动到另一个页面(例如,process.php)以希望简化;对此有何意见?最佳/常见做法是什么?

提前非常感谢,

svs

I have a form page in which either an INSERT or an UPDATE query is submitted, depending on the presence/absence of an ID (and when there's an ID it's used to retrieve the record and pre-populate the form). In either case, the processing is in form.php so the form's action is itself (action="/form.php">). My problem is that when form.php reloads post-submit, the URL has an empty ID so the page enters 'INSERT' mode, rather than 'UPDATE' mode. What's the best practice way to resolve this?

  1. What operator/condition should I add to this 'if' ...

    if (isset($_GET['ID']) && is_numeric($_GET['ID'])) {

... to include post-submit empty ID URL (i.e., form.php?ID=)

OR,

  1. How do I pass `$newID = mysql_insert_id();1 to the form's action? (I've tried a number of variations here w/out success)

    $newID = mysql_insert_id();
    ... [ snip ] ...
    <form method="post" action="/html/form.php?ID=<?php echo $newID; ?>">

I'm reading about hidden inputs and sessions but it's not yet clear to me how to use either to solve this problem. Lastly, since it isn't absolutely necessary that I reload the form page, I'm increasingly tempted to move the form processing/db queries to another page (e.g., process.php) to hopefully simplify; any opinions on this? What's best/common practice?

Many thanks in advance,

svs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极致的悲 2024-12-14 19:26:37

常见的做法应该是将数据发布与数据显示分开。这可以防止用户首次到达页面时意外添加内容,以及用户点击刷新时意外重复发布。

此外,保持逻辑分离可以使代码在将来更具可读性和可维护性。

您可能应该寻找的方法是:

view.php?ID=<record to view> // Only displays a record already in the DB
add.php                      // The add record form with action="process_add.php"
process_add.php?Field1=<>&Field2=<>... // Receives data from add.php, puts it in
                                       // the database and then forwards back to
                                       // view.php or add.php as you see fit.

编辑:虽然我在 process_add.php 上有 GET 参数,但它们只是用来证明它们正在被传递。它们应该在实际实现中作为 POST 参数发送。

Common practice should be to keep data posting separate from data displaying. This prevents accidental adds on a user's first arrival to the page as well as accidental double-posts if the user hits refresh.

In addition, keeping the logic separate makes the code more readable and maintainable in the future.

The approach you should probably look for is:

view.php?ID=<record to view> // Only displays a record already in the DB
add.php                      // The add record form with action="process_add.php"
process_add.php?Field1=<>&Field2=<>... // Receives data from add.php, puts it in
                                       // the database and then forwards back to
                                       // view.php or add.php as you see fit.

EDIT: While I have GET arguments on process_add.php, they are only there to demonstrate that they are being passed. They should be sent as POST arguments in and actual implementation.

一花一树开 2024-12-14 19:26:37

这是使用模板的此类代码的示例。
工作 CRUD 应用程序基于传递 id 的想法

,但不知道为什么你需要传递新生成的 id。

<?  
mysql_connect(); 
mysql_select_db("new"); 
$table = "test"; 
if($_SERVER['REQUEST_METHOD']=='POST') { //form handler part: 
  $name = mysql_real_escape_string($_POST['name']); 
  if ($id = intval($_POST['id'])) { 
    $query="UPDATE $table SET name='$name' WHERE id=$id"; 
  } else { 
    $query="INSERT INTO $table SET name='$name'"; 
  } 
  mysql_query($query) or trigger_error(mysql_error()." in ".$query); 
  header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);  
  exit;  
}  
if (!isset($_GET['id'])) { //listing part: 
  $LIST=array(); 
  $query="SELECT * FROM $table";  
  $res=mysql_query($query); 
  while($row=mysql_fetch_assoc($res)) $LIST[]=$row; 
  include 'list.php'; 
} else { // form displaying part: 
  if ($id=intval($_GET['id'])) { 
    $query="SELECT * FROM $table WHERE id=$id";  
    $res=mysql_query($query); 
    $row=mysql_fetch_assoc($res); 
    foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v); 
  } else { 
    $row['name']=''; 
    $row['id']=0; 
  } 
  include 'form.php'; 
}  
?>

模板:
form.php

<? include TPL_TOP ?>
<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
<a href="?">Return to the list</a>
</form>
<? include TPL_BOTTOM ?>

和 list.php:

<? include TPL_TOP ?>
<a href="?id=0">Add item</a>
<? foreach ($LIST as $row): ?>
<li><a href="?id=<?=$row['id']?>"><?=$row['name']?></a>
<? endforeach ?>
<? include TPL_BOTTOM ?>

here is an example of such a code, using templates.
working CRUD application based on the idea of passing id

dunno, though, why do you need to pass freshly generated id.

<?  
mysql_connect(); 
mysql_select_db("new"); 
$table = "test"; 
if($_SERVER['REQUEST_METHOD']=='POST') { //form handler part: 
  $name = mysql_real_escape_string($_POST['name']); 
  if ($id = intval($_POST['id'])) { 
    $query="UPDATE $table SET name='$name' WHERE id=$id"; 
  } else { 
    $query="INSERT INTO $table SET name='$name'"; 
  } 
  mysql_query($query) or trigger_error(mysql_error()." in ".$query); 
  header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);  
  exit;  
}  
if (!isset($_GET['id'])) { //listing part: 
  $LIST=array(); 
  $query="SELECT * FROM $table";  
  $res=mysql_query($query); 
  while($row=mysql_fetch_assoc($res)) $LIST[]=$row; 
  include 'list.php'; 
} else { // form displaying part: 
  if ($id=intval($_GET['id'])) { 
    $query="SELECT * FROM $table WHERE id=$id";  
    $res=mysql_query($query); 
    $row=mysql_fetch_assoc($res); 
    foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v); 
  } else { 
    $row['name']=''; 
    $row['id']=0; 
  } 
  include 'form.php'; 
}  
?>

templates:
form.php

<? include TPL_TOP ?>
<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
<a href="?">Return to the list</a>
</form>
<? include TPL_BOTTOM ?>

and list.php:

<? include TPL_TOP ?>
<a href="?id=0">Add item</a>
<? foreach ($LIST as $row): ?>
<li><a href="?id=<?=$row['id']?>"><?=$row['name']?></a>
<? endforeach ?>
<? include TPL_BOTTOM ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文