2016-04-02 41 views
0

我在這裏有一些小問題。在提交時插入MySQL數據

數據會插入數據庫,但當我打開頁面時會插入數據。

我想要在按下提交按鈕時插入數據,而不是在頁面加載/刷新時插入數據。希望可以有人幫幫我!

下面是代碼:

<html> 
    <head> 
    </head> 

    <body> 
    <h2>Arbeidsliste</h2> 

    <div id ="insert"> 
     <form action="index.php" method="post"> 
     <p> 
      <label for="dato">Dato: </label> 
      <input type="date" name="dato" class="field"> 
     </p> 
     <p> 
      <label for="start">Start: </label> 
      <input type="time" name="start" class="field"> 
     </p> 
     <p> 
      <label for="slutt">Slutt: </label> 
      <input type="time" name="slutt" class="field"> 
     </p> 
     <p> 
      <label for="pause">Pause: </label> 
      <input type="number" name="pause" class="field"> 
     </p> 

     <p> 
      <input type="submit"> 
     </p> 
     </form> 
    </div> 
    <?php 
     $servername = "localhost"; 
     $username = "root"; 
     $password = ""; 
     $dbname = "timer"; 

     // Create connection 
     $conn = new mysqli($servername, $username, $password, $dbname); 
     // Check connection 
     if ($conn->connect_error) { 
      die("Connection failed: " . $conn->connect_error); 
     } 

     $sql = "INSERT INTO meny (dato, start, slutt, pause) 
       VALUES ('2016-04-01', '12:00', '20:00', '30')"; 

     if ($conn->query($sql) === TRUE) { 
      echo "New record created successfully"; 
     } else { 
      echo "Error: " . $sql . "<br>" . $conn->error; 
     } 

     $conn->close(); 
    ?> 
    </body> 
</html> 
+0

基本'if(){...}'。並且我們無法確定要綁定的內容。 –

+0

請向我們展示與此相關的HTML。 –

+1

我會說弗雷德 - 我的答案是你想要的。 –

回答

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

    // execute query 

} 

此基礎上對相同的名稱屬性的輸入,併爲您的表單POST方法。

<input type="submit" name="submit" value="Submit"> 

另外,如果你得到來自用戶的輸入,你將需要檢查empty()領域和使用事先準備好的聲明。

編輯:根據您的編輯。

<input type="submit">更改爲我上面列出的內容。

空場基本檢查:

if(!empty($_POST['dato']) && !empty($_POST['start'])){ 

    $dato = $_POST['dato']; 
    $start = $_POST['start']; 

    // execute the query 

} 

並應用相同的邏輯在其他領域。

哦,只是要確定...我會添加一些額外的條件在這裏:

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

    if(!empty($_POST['dato']) && !empty($_POST['start'])){ 

     $dato = $_POST['dato']; 
     $start = $_POST['start']; 

     // execute the query 

    } 

} 

,你可以添加一個頭重定向。

在功能上讀了起來:

,並確保你沒有頭之前輸出。如果你得到一個警告,請在堆棧以下幾點:

或者說,如何在PHP重定向:

你有另一種選擇,那就是使用兩個單獨的文件。

一個用於您的HTML表單,另一個用於PHP/MySQL,但在成功查詢後仍應使用標題進行重定向。

  • 令牌/會話是另一個可行的選擇。
+0

這將無法正常工作,它仍然會重新加載頁面。 – 4castle

+0

@ 4castle * huh?!*我已經完成了多少次。 –

+0

他們希望在不重新加載頁面的情況下提交表單。這不能解決這個問題。 – 4castle

0

a if (isset($_POST['submit'])) { your php code }如果我解釋你的問題,我會做的。

<html> 
    <head> 
    </head> 

    <body> 
    <h2>Arbeidsliste</h2> 

    <div id ="insert"> 
     <form action="index.php" method="post"> 
     <p> 
      <label for="dato">Dato: </label> 
      <input type="date" name="dato" class="field"> 
     </p> 
     <p> 
      <label for="start">Start: </label> 
      <input type="time" name="start" class="field"> 
     </p> 
     <p> 
      <label for="slutt">Slutt: </label> 
      <input type="time" name="slutt" class="field"> 
     </p> 
     <p> 
      <label for="pause">Pause: </label> 
      <input type="number" name="pause" class="field"> 
     </p> 

     <p> 
      <input type="submit"> 
     </p> 
     </form> 
    </div> 
    <?php 

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

     $servername = "localhost"; 
     $username = "root"; 
     $password = ""; 
     $dbname = "timer"; 

     // Create connection 
     $conn = new mysqli($servername, $username, $password, $dbname); 
     // Check connection 
     if ($conn->connect_error) { 
      die("Connection failed: " . $conn->connect_error); 
     } 

     $sql = "INSERT INTO meny (dato, start, slutt, pause) 
       VALUES ('2016-04-01', '12:00', '20:00', '30')"; 

     if ($conn->query($sql) === TRUE) { 
      echo "New record created successfully"; 
     } else { 
      echo "Error: " . $sql . "<br>" . $conn->error; 
     } 

     $conn->close(); 
}   
?> 
    </body> 
</html>