2015-07-18 41 views
1

長時間閱讀器,第一次張貼海報。我是PHP新手,我有一個我一直在工作的頁面。現在我有數據庫連接運行良好,我的SELECT語句給我需要的信息。我的問題是雙重的(這個帖子後,或許更多; 設置您的移相器畏縮):疑難解答HTML和PHP/MySQL

  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>

+1

你沒有做任何錯誤您的查詢檢查,你只是認爲他們會工作。檢查你的錯誤日誌。 –

+0

你可以檢查你的插入查詢是否存在這樣的錯誤:$ i_result = mysqli_query($ connection,$ insert)或trigger_error($ connection-> error。「[$ insert]」);' – Shehary

回答

3

這兩條線將失敗:這裏

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

兩個問題,都與第二行:

  • 之間沒有空格), LIMIT:)LIMIT 1是你的代碼;
  • LIMIT 1在INSERT不允許....
+0

已經解決了問題1 ...謝謝! – BigTime

+0

任何人有關於如何防止瀏覽器重新發送表單輸入數據的想法?我曾嘗試添加 未設置($ _ POST); $ _POST = array(); 到if語句的底部無濟於事。 – BigTime

+0

您無法重置POST,因爲瀏覽器會再次生成它。我將使用一個單獨的php腳本來處理表單(所以只需在兩個腳本中拆分頁面,並在窗體處理完成後使用window.location來加載表單)。 –