HTML和PHP / MySQL故障排除

时间:2015-07-18 23:56:46

标签: php html mysql forms post

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

  1. 有一次,我让INSERT工作,但它突然停止了,没有多少调整似乎把它带回来。我已经验证INSERT语句在没有变量的单独PHP文件中工作。

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

  3. <?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>

1 个答案:

答案 0 :(得分:3)

这两行将失败:

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

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

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