【问题标题】:Form submission weird behaviour [duplicate]表单提交奇怪的行为[重复]
【发布时间】:2019-01-20 16:16:38
【问题描述】:
<?php
require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("index.php");
exit;
}

include 'header.php';
include './include/sql/connect.php';

if(isset($_POST['submit']))
{
$insert = "INSERT INTO `customers`(`bb_id`, `name`, `email`, `phone`, `circle`, `ssa`, `sdca`) VALUES (?,?,?,?,?,?,?)";

    $stmt = mysqli_prepare($connect, $insert);
    $stmt->bind_param('sssisss', $_POST['bb_id'],$_POST['name'],$_POST['email'],$_POST['phone'],$_POST['circle'],$_POST['ssa'],$_POST['sdca']);
    $stmt->execute();

    if (!$stmt)
    {
        printf("Error: %s\n", mysqli_error($connect));
        exit();
    }
}


$cust = "SELECT * FROM `customers`";
$qry = mysqli_query($connect,$cust);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Customers</title>
    <!--link rel="stylesheet" type="text/css" href="style/fg_membersite.css"-->
    <link rel="stylesheet" type="text/css" href="style/tableview.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<br>
<div id='tableview'>
    <div class='ext-box'>
        <form action="" method="post">
            <input type="text" name="bb_id" placeholder="Broadband ID">&nbsp;
            <input type="text" name="name" placeholder="Name">&nbsp;
            <input type="text" name="email" placeholder="Email">&nbsp;
            <input type="text" name="phone" placeholder="Phone">&nbsp;
            <input type="text" name="circle" placeholder="Circle">&nbsp;
            <input type="text" name="ssa" placeholder="SSA">&nbsp;
            <input type="text" name="sdca" placeholder="SDCA"><br><br>
            <input type="submit" name="searchsubmit" value="Search Records">
            <input type="submit" name="submit" value="Update Records">
        </form>
    </div>
    <br><br><br>
</div>

<table class="blueTable">
<thead>
    <tr>
        <th>Broadband ID</th>
        <th>Name</th>
        <th>Email</th>
        <th>Phone</th>
        <th>Circle</th>
        <th>SSA</th>
        <th>SDCA</th>
    </tr>
</thead>
<tfoot>
    <tr>
        <td colspan="7">
            <div class="links"><a href="#">&laquo;</a> <a class="active" href="#">1</a> <a href="#">2</a> <a href="#">3</a> <a href="#">4</a> <a href="#">&raquo;</a></div>
        </td>
    </tr>
</tfoot>
<tbody>
    <?php while($row = mysqli_fetch_array($qry)):?>
    <tr>
        <td><?php echo $row['bb_id'];?></td>
        <td><?php echo $row['name'];?></td>
        <td><?php echo $row['email'];?></td>
        <td><?php echo $row['phone'];?></td>
        <td><?php echo $row['circle'];?></td>
        <td><?php echo $row['ssa'];?></td>
        <td><?php echo $row['sdca'];?></td>
    </tr>
    <?php endwhile;?>
</tbody>
</table>            


</body>
</html>

上面的代码应该将表单中填写的数据插入到数据库中。它确实做到了,但奇怪的只是有时。

就像我尝试使用 chrome 插入数据 2 小时以来,但它不会这样做。突然间,我用 IE 做了同样的事情,它接受了插入。我想也许是浏览器兼容性,但是当我再次尝试使用 IE 时,它在执行一次后停止插入。

【问题讨论】:

  • 1) 请消除会员代码。 2) 向我们展示来自 connect.php 的代码。 3) 在顶部添加error_reporting(E_ALL); ini_set('display_errors', 1); 以显示最终错误。 4) 使用&lt;!DOCTYPE html&gt; &lt;html&gt;。然后再次给我们您的反馈。

标签: php html mysql forms submit


【解决方案1】:

您没有检查执行是否正确成功。

变化:

$stmt->execute();
if (!$stmt) {
    ...
}

到:

if (!$stmt->execute()) {
    ...
}

【讨论】:

  • 非常感谢。所以,似乎我试图插入一个 BIGINT 来代替 INT。现在我可以正确插入查询,有点。现在出现另一个错误,错误:键“PRIMARY”的重复条目。即使我输入的字符串不存在。查询给出了这个错误,但也插入了数据。
  • 那么你有一个主键冲突。如果您希望能够存储重复的行,则需要删除主键。现在,第一个被插入,随后的插入被拒绝。
  • @MohammadOzairAkhlaq 确保您只调用一次 $stmt-&gt;execute()
  • @MohammadOzairAkhlaq 我见过很多像$stmt-&gt;execute(); if ($stmt-&gt;execute()) ...这样的错误
  • @mickmackusa 但如果字符串不存在,为什么首先会出现重复错误?不,我不想存储重复项,这就是我将其设为 PK 的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 1970-01-01
相关资源
最近更新 更多