【问题标题】:Auto generated (also auto increment) input field in a form表单中自动生成(也自动递增)的输入字段
【发布时间】:2016-08-23 06:25:23
【问题描述】:

<form action="" method="POST">
  <input type="text" name="bill_no" placeholder="Bill no" />
  <input type="text" name="patient_name" placeholder="Patient name" />
  <input type="date" name="bill_date">
  <input type="submit" name="submit" value="Submit">
</form>

PHP 代码:

<?php 
// Connecting to database 
    include "connect.php"; 
if (ISSET($_POST['submit'])) 
{ 
       $bill_no = $con->real_escape_string($_POST['bill_no']); 
       $patient_name = $con->real_escape_string($_POST['patient_name']); 
       $bill_date = $con->real_escape_string($_POST['bill_date']); 
       $query = "INSERT INTO bills(bill_no, patient_name, bill_date) VALUES (?,?,?)"; 
       $stmt = $con->prepare($query); 
       $stmt->bind_param("sss", $bill_no, $patient_name, $bill_date ); 
       $stmt->execute(); } 
       $con->close(); 
?>

我有一个可供多个用户一次访问的表单。在我的表单中,如果用户未提交表单或意外关闭,我需要一个字段 (say bill_no),当用户 打开 表单时,该字段应该自动递增浏览器窗口的值应该被破坏并且不应该保存在我的数据库中。 bill_no 应在用户打开表单时立即显示,该表单应以之前的 bill_no 递增 1。谢谢。

【问题讨论】:

  • 请输入您的代码,您尝试了什么。
  • @rahul 实际上我不知道如何解决我的问题。我只有这个概念。
  • 你的意思是不提交表单,ID值自动增加??为下一个用户
  • @KarthiVenture 是的,如果用户没有提交表单或关闭浏览器,则不应保存该值
  • 可以发php代码吗

标签: javascript php jquery html forms


【解决方案1】:

试试这个可能对你有帮助。提交后它将重定向您的页面,因此不会在第二次插入旧数据云

$newURL="youfile.php";
if (ISSET($_POST['submit'])) 
{ 
       $bill_no = $connection->real_escape_string($_POST['bill_no']); 
       $patient_name = $connection->real_escape_string($_POST['patient_name']); 
       $bill_date = $connection->real_escape_string($_POST['bill_date']); 
       $query = "INSERT INTO bills(bill_no, patient_name, bill_date) VALUES (?,?,?)";
       $stmt = $connection->prepare($query); 
       $stmt->bind_param("sss", $bill_no, $patient_name, $bill_date ); 
       $stmt->execute();
     header('Location: '.$newURL);
 } 
$connection->close(); 

更多了解HEADER in PHP

【讨论】:

    【解决方案2】:

    在表单页面上创建一个选择查询以获取您的账单编号。将其保存在数据库中并显示在您的账单号的输入值上。增量(+1)。

    这可以是逻辑并在后端检查(在插入数据库之前)如果账单号已经在数据库中,然后重定向回表单并带有一些错误消息“最后一个账单号。已经存在”之类的然后让他提交表格,因为我们已经动态设置了账单号。

    试试这个

    【讨论】:

      猜你喜欢
      • 2012-07-21
      • 2015-07-30
      • 1970-01-01
      • 2019-07-26
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      相关资源
      最近更新 更多