当您打开它时,表单尝试提交到数据库

问题描述 投票:0回答:1

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

 $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);*/

这是它正在做的事情

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  | [email protected]              | 2019-03-01 10:30:21.161783
  5 | Tim555 | Tim           | Tiny         | 666 Q St        | Hope     | AR        | 77858   | 222-222-2222  | 333-333-3333  | [email protected]          | 2019-03-01 10:33:37.960887
  6 | a      | b             | c            | d               | e        | f         | 666     | 6666          | 6666          | [email protected]               | 2019-03-01 10:34:08.699302
  8 | e      | f             | g            | h               | i        | j         | 44      | 55            | 66            | [email protected]               | 2019-03-01 10:35:06.950209
(6 rows)

`重新打开或刷新时,您可以看到1和3以及6和8之间的跳过

表格代码

<form action="" method="post">
CustID:<input type="text" name="custID"><br>
Fist Name: <input type="text" name="CustFirstName"><br>
Last Name: <input type="text" name="CustLastName"><br>
Address: <input type="text" name="CustAddress"><br>
City: <input type="text" name="CustCity"><br>
State: <input type="text" name="CustState"><br>
Zip Code: <input type="text" name="CustZip"><br>
Cell Phone: <input type="text" name="CustCellPhone"><br>
Work Phone: <input type="text" name="CustWorkPhone"><br>
E-mail: <input type="text" name="CustEmail"><br>
<input type="submit">
</form>
php pdo postgresql-10
1个回答
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();
}
© www.soinside.com 2019 - 2024. All rights reserved.