【问题标题】:Troubleshooting HTML and PHP / MySQLHTML 和 PHP / MySQL 故障排除
【发布时间】:2015-10-08 09:46:23
【问题描述】:

长期阅读,第一次海报。我是一个新手 PHP 爱好者,我有一个我一直在工作的页面。现在我的数据库连接运行良好,我的 SELECT 语句给了我需要的信息。我的问题有两个(可能在这篇文章之后更多;将你的移相器设置为畏缩):

  1. 在某一时刻,我让 INSERT 工作,但它突然停止了,似乎没有任何调整可以将其恢复。我已经验证 INSERT 语句在没有变量的单独 PHP 文件中工作。

  2. 当我确实让 INSERT 工作时,每次刷新页面都会复制最后一个条目。我尝试了几种方法来清除 $_POST 数组,但我认为我的一些实验会导致问题 #1。

<?php 
$dbhost = "REDACTED";
$dbuser = "REDACTED";
$dbpass = "REDACTED";
$dbname = "guest_list";
// Create a database connection
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection succeeded
if(mysqli_connect_errno()) {
  die("DB's not here, man: " . 
      mysqli_connect_error() . 
      " (" . mysqli_connect_errno() . ")"
     );
}
// replacement for mysql_real_escape_string()
function html_escape($html_escape) {
  $html_escape =  htmlspecialchars($html_escape, ENT_QUOTES | ENT_HTML5, 'UTF-8');
  return $html_escape;
}

// Posting new data into the DB
if (isset($_POST['submit'])) {
  $first = html_escape($_POST['first']);
  $last = html_escape($_POST['last']);
  $contact = html_escape($_POST['contact']);
  $associate = html_escape($_POST['associate']);

  $insert = "INSERT INTO g_list (";
  $insert .= "g_fname, g_lname, g_phone, g_association) ";
  $insert .= "VALUES ('{$first}', '{$last}', '{$contact}', '{$associate}')";
  $insert .= "LIMIT 1";
  $i_result = mysqli_query($connection, $insert);
// I have verified that the above works by setting the varialble 
// in the VALUES area to strings and seeing it update
}

$query  = "SELECT * ";
$query .= "FROM g_list ";
$query .= "ORDER BY g_id DESC";
$q_result = mysqli_query($connection, $query);

?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Guest List</title>
    <link href="guest.css" media="all" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <header>
      <h1>REDACTED</h1>
      <h2>Guest Registry</h2>
    </header>
    <div class="container">
      <div class="registry">
        <form name="formup" id="main_form" method="post">
          <fieldset>
            <legend>Please enter your name into the registry</legend>
            <p class="first">First Name: 
              <input type="text" name="first" value="" placeholder="One or more first names" size="64"></p>
            <p class="last">Last Name:
              <input type="text" name="last" value="" placeholder="Last name" size="64"></p>
            <p class="contact">Phone Number or Email:
              <input type="text" name="contact" value="" placeholder="" size="32"></p>
            <p class="associate">Your relation?
              <input type="text" name="associate" value="" placeholder="" size="128"></p>
            <p class="submit">
              <input type="submit" name="submit" title="add" value="submit" placeholder=""></p>
          </fieldset>
        </form>
      </div>
    </div>

    <h3>Guest List:</h3>            
    <table>
      <tr>
        <th>Firstname(s)</th><th>Lastname</th>
        <th>Phone or Email</th><th>Association</th>
      </tr>

      <?php while($guest = mysqli_fetch_assoc($q_result)) {
  echo "<tr>" . "<td>" . $guest["g_fname"] . "</td>"
    . "<td>" . $guest["g_lname"] . "</td>"
    . "<td>" . $guest["g_phone"] . "</td>"
    . "<td>" . $guest["g_association"] . "</td>" . "</tr>";
} ?>

    </table>
    <footer>
      <div>Copyright <?php echo date("Y"); ?>, REDACTED, LLC.</div>

      <?php
if (isset($connection)) {
  mysqli_close($connection);
}
      ?>
    </footer>
  </body>
</html>

【问题讨论】:

  • 您没有对查询进行任何错误检查,您只是假设它们会起作用。检查您的错误日志。
  • 你可以检查你的插入查询是否有这样的错误$i_result = mysqli_query($connection, $insert) or trigger_error($connection-&gt;error."[$insert]");

标签: php html mysql forms post


【解决方案1】:

这两行将失败:

$insert .= "VALUES ('{$first}', '{$last}', '{$contact}', '{$associate}')";
$insert .= "LIMIT 1";

这里有两个问题,都在第二行:

  • ) 和 LIMIT 之间没有空格:)LIMIT 1 是您的代码;
  • 不允许在 INSERT 中使用 LIMIT 1....

【讨论】:

  • 有人对如何防止浏览器重新发送表单输入数据有任何想法吗?我试过添加 unset($_POST); $_POST = 数组();到if语句的底部无济于事。
  • 您无法重置 POST,因为浏览器只会再次生成它。我会使用一个单独的 php 脚本来处理表单(所以只需将您的页面拆分为两个脚本,并在处理完表单后使用 window.location 来加载表单)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-18
  • 2010-12-12
  • 2012-09-28
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多