【问题标题】:Prepared statement returning false but query was successful准备好的语句返回 false 但查询成功
【发布时间】:2018-10-05 05:06:24
【问题描述】:

我正在使用带有准备好的语句的脚本在我的数据库中创建一条记录,在这种情况下创建一个客户。

我想在脚本中添加一个检查以查看查询是否成功运行。在做了一些研究后,我遇到了this StackOverflow 帖子。 execute(); 返回一个布尔值,所以我可以添加 if($stmt-execute()),这是有道理的。

我添加了下面的代码来查看我的查询是否成功运行,添加一个类并相应地显示一条消息。

if($stmt->execute()) {
  $style = "class='succes'";
  $msg = "De klant is toegevoegd!";

  echo "<div " . $style . ">";
  echo "<p>" . $msg . "</p>";
  echo "</div>";

  } else {
    $style = "class='fail'";
    $msg = "De klant kon niet worden toegevoegd, probeer het later opnieuw.";

    echo "<div " . $style . ">";
    echo "<p>" . $msg  . "</p>";
    echo "</div>";
  }
}

我的总代码现在看起来像这样。

<?php
if(isset($_POST['submit'])) {

  require('dbconfig.php');

  $stmt = $connect->prepare('INSERT INTO `customers` (customer_name, customer_type, customer_address, customer_postal, customer_phone, customer_email, customer_company, customer_city) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');

  if($stmt) {
    $name = $_POST['customerName'];
    $type = $_POST['customerType'];
    $address = $_POST['customerAddress'];
    $postal = $_POST['customerPostal'];
    $phone = $_POST['customerPhone'];
    $email = $_POST['customerEmail'];
    $company = $_POST['customerCompany'];
    $city = $_POST['customerCity'];

    $stmt->bind_param('ssssssss', $name, $type, $address, $postal, $phone, $email, $company, $city);

    $stmt->execute();
    $connect->close();
  }
}

?>

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>DETACHIT - ADD CUSTOMER</title>

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href="https://fonts.googleapis.com/css?family=Oxygen" rel="stylesheet">
    <link rel="stylesheet" href="./css/detachit-webapp.css">
    <link rel="icon" href="./img/favicon.ico">
  </head>
  <body>
    <div class="container">
      <div class="top-navbar">
        <ul>
          <li><a href="webapp.php"><i class="fa fa-home"></i></a></li>
          <li><a href="settings.php"><i class="fa fa-cogs"></i></a></li>
          <li><a href="my-account.php"><i class="fa fa-user"></i></a></li>
          <li><a onclick="pageBack()"><i class="fa fa-arrow-left"></i></a></li>
          <li><a onclick="pageForward()"><i class="fa fa-arrow-right"></i></a></li>
          <li style="float:right"><a href="logout.php">Uitloggen</a></li>
        </ul>
      </div>
      <div class="inner-container">
        <div class="left-inner-container">
          <form class="webapp-form" method="post">
            <h3>Klant toevoegen</h3>
            <input type="text" name="customerName" placeholder="Naam" value="">
            <select name="customerType">
              <option value="0">Selecteer...</option>
              <option value="Particulier">Particulier</option>
              <option value="Bedrijf">Bedrijf</option>
              <option value="Anders">Anders</option>
            </select>
            <input type="text" name="customerAddress" placeholder="Straat en huisnummer">
            <input type="text" name="customerPostal" placeholder="Postcode">
            <input type="text" name="customerPhone" placeholder="Telefoonnummer">
            <input type="text" name="customerEmail" placeholder="Email adres">
            <input type="text" name="customerCompany" placeholder="Bedrijfsnaam">
            <input type="text" name="customerCity" placeholder="Plaats">
            <input type="submit" name="submit" value="Aanmaken">
          </form>
        </div>
        <div class="right-inner-container">
          <h3>Klanten toevoegen</h3>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
          </p>
          <?php

          if(isset($_POST['submit'])) {


            if($stmt->execute()) {
              $style = "class='succes'";
              $msg = "De klant is toegevoegd!";

              echo "<div " . $style . ">";
              echo "<p>" . $msg . "</p>";
              echo "</div>";

            } else {
              $style = "class='fail'";
              $msg = "De klant kon niet worden toegevoegd, probeer het later opnieuw.";

              echo "<div " . $style . ">";
              echo "<p>" . $msg  . "</p>";
              echo "</div>";
            }
          }

          ?>
        </div>
      </div>
    </div>
  </body>
</html>

但这就是问题的开始。我测试了我的脚本,它返回了错误消息。为了确保它失败了,我检查了我的数据库,但记录在那里。

为什么当我的查询成功运行时if($stmt-&gt;execute()) 返回FALSE

【问题讨论】:

  • 你跑了$stmt-&gt;execute()两次,你对第二个有什么期望,嗯?

标签: php mysql prepared-statement


【解决方案1】:

您需要删除 $stmt-&gt;execute();,因为该语句在 if 语句中执行。

它现在做了两次,这是行不通的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-13
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    相关资源
    最近更新 更多