表单尝试在打开数据库时提交到数据库

时间:2019-03-01 16:58:33

标签: php pdo postgresql-10

当我打开表单页面时,它尝试立即提交到数据库,并导致主键字段中的跳过。如果我提交连续的输入,则不会导致跳过。如果我关闭然后重新打开,它将跳过。有什么想法吗?

implementation 'android.arch.navigation:navigation-fragment:1.0.0-rc02'
implementation 'android.arch.navigation:navigation-ui:1.0.0-rc02'

这是它在做什么

 $dbh = new PDO("pgsql:host=$host;dbname=$dbname", $dbuser, $dbpass);
if(!$dbh) {
      echo "Error : Unable to open database\n";
   } else {
      echo "Opened database successfully\n";
   }

if (!empty($_POST['submit'])) {
$custID = $_POST['custID'];
$CustFirstName = $_POST['CustFirstName'];
$CustLastName = $_POST['CustLastName'];
$CustAddress =$_POST['CustAddress'];
$CustCity = $_POST['CustCity'];
$CustState = $_POST['CustState'];
$CustZip = $_POST['CustZip'];
$CustCellPhone = $_POST['CustCellPhone'];
$CustWorkPhone = $_POST['CustWorkPhone'];
$CustEmail = $_POST['CustEmail'];
}
$sql = "INSERT INTO customers (custID, CustFirstName, CustLastName, CustAddress, CustCity, CustState, CustZip, CustCellPhone, CustWorkPhone, CustEmail) 
VALUES (:custID, :CustFirstName, :CustLastName, :CustAddress, :CustCity, :CustState, :CustZip, :CustCellPhone, :CustWorkPhone, :CustEmail)";

$stmt = $dbh->prepare($sql);
$stmt->bindValue(':custID', $_POST['custID']);
$stmt->bindValue(':CustFirstName', $_POST['CustFirstName']);
$stmt->bindValue(':CustLastName', $_POST['CustLastName']);
$stmt->bindValue(':CustAddress', $_POST['CustAddress']);
$stmt->bindValue(':CustCity', $_POST['CustCity']);
$stmt->bindValue(':CustState', $_POST['CustState']);
$stmt->bindValue(':CustZip', $_POST['CustZip']);
$stmt->bindValue(':CustCellPhone', $_POST['CustCellPhone']);
$stmt->bindValue(':CustWorkPhone', $_POST['CustWorkPhone']);
$stmt->bindValue(':CustEmail', $_POST['CustEmail']);
$inserted = $stmt->execute();


/*
var_dump($_POST);*/

` 重新打开或刷新时,您会看到1&3和6&8之间的跳跃

表格代码

accounting=# select * from customers;
 id | custid | custfirstname | custlastname |   custaddress   | custcity | custstate | custzip | custcellphone | custworkphone |       custemail       |             ts             
----+--------+---------------+--------------+-----------------+----------+-----------+---------+---------------+---------------+-----------------------+----------------------------
  1 | Swo750 | Pat           
  3 | Bod750 | Bob          
  4 | Col555 | Sam           | Close        | 5525 East       | West     | TX        | 76052   | 555-555-5555  | 888-555-5555  | b@ya.com              | 2019-03-01 10:30:21.161783
  5 | Tim555 | Tim           | Tiny         | 666 Q St        | Hope     | AR        | 77858   | 222-222-2222  | 333-333-3333  | tim@tiny.com          | 2019-03-01 10:33:37.960887
  6 | a      | b             | c            | d               | e        | f         | 666     | 6666          | 6666          | g@g.com               | 2019-03-01 10:34:08.699302
  8 | e      | f             | g            | h               | i        | j         | 44      | 55            | 66            | j@j.com               | 2019-03-01 10:35:06.950209
(6 rows)

1 个答案:

答案 0 :(得分:0)

无论表单是否提交,都将执行您的SQL代码。将$stmt执行移到if (!empty($_POST['submit']))块内。

$dbh = new PDO("pgsql:host=$host;dbname=$dbname", $dbuser, $dbpass);
if(!$dbh) {
      echo "Error : Unable to open database\n";
   } else {
      echo "Opened database successfully\n";
   }

if (!empty($_POST['submit'])) {
  $custID = $_POST['custID'];
  $CustFirstName = $_POST['CustFirstName'];
  $CustLastName = $_POST['CustLastName'];
  $CustAddress =$_POST['CustAddress'];
  $CustCity = $_POST['CustCity'];
  $CustState = $_POST['CustState'];
  $CustZip = $_POST['CustZip'];
  $CustCellPhone = $_POST['CustCellPhone'];
  $CustWorkPhone = $_POST['CustWorkPhone'];
  $CustEmail = $_POST['CustEmail'];

  $sql = "INSERT INTO customers (custID, CustFirstName, CustLastName, CustAddress, CustCity, CustState, CustZip, CustCellPhone, CustWorkPhone, CustEmail) 
VALUES (:custID, :CustFirstName, :CustLastName, :CustAddress, :CustCity, :CustState, :CustZip, :CustCellPhone, :CustWorkPhone, :CustEmail)";

  $stmt = $dbh->prepare($sql);
  $stmt->bindValue(':custID', $_POST['custID']);
  $stmt->bindValue(':CustFirstName', $_POST['CustFirstName']);
  $stmt->bindValue(':CustLastName', $_POST['CustLastName']);
  $stmt->bindValue(':CustAddress', $_POST['CustAddress']);
  $stmt->bindValue(':CustCity', $_POST['CustCity']);
  $stmt->bindValue(':CustState', $_POST['CustState']);
  $stmt->bindValue(':CustZip', $_POST['CustZip']);
  $stmt->bindValue(':CustCellPhone', $_POST['CustCellPhone']);
  $stmt->bindValue(':CustWorkPhone', $_POST['CustWorkPhone']);
  $stmt->bindValue(':CustEmail', $_POST['CustEmail']);
  $inserted = $stmt->execute();
}