2014-04-20 205 views
0

試圖用我以前的線程自己弄清楚這一點,但其他人的例子並沒有幫助我弄清楚我需要什麼。獨立文件本身工作,數據插入到數據庫中就好了......但是我試圖將這些文檔作爲一個整合的單一PHP文檔來適應。使用PHP POST方法張貼到同一頁

我想我會拋出我正在嘗試做的和我到目前爲止的代碼,也許有人會明白我想要完成什麼,並能夠解釋如何去做。

我基本上有一個form(form.php)的PHP文檔,它使用POST將表單數據發送到第二個PHP文檔,該文檔又將表單數據發送到MySQL數據庫。我希望用戶能夠使用同一文檔發佈數據,並重置數據字段,以便用戶和提交通過表單輸入更多信息。我希望讓用戶知道由於字段不完整而導致的錯誤,並且我希望用戶知道成功的條目(我將使用echo函數)。

form.php的是如下:

<html> 
<head> 
    <link rel="stylesheet" type="text/css" href="form.css"> 
    <title>MySQL Database Creation System</title> 
    <script src="jquery-1.11.0.min.js"></script> 
</head> 
<body> 
    <div class="wrapper"> 
     <img src="eris.png" align="middle"> 
     <form action="insert.php" method="post"> 
     Firstname: <input type="text" size="16" name="firstname"> 
     Lastname: <input type="text" size="16" name="lastname"> 
     Age: <input type="text" size="1" maxlength="3" name="age"> 
     <input type="submit" class="button"> 
     </form> 
    </div> 
</body> 

insert.php如下:提前爲你可以給任何幫助

<html> 
<head> 
    <link rel="stylesheet" type="text/css" href="insert.css"> 
    <title>MySQL Database Creation System</title> 
</head> 
<body> 
    <div class="wrapper"> 
    <img src='eris.png' align='middle'> 
     <?php 
     if ((trim($_POST['firstname']) == "") || (trim($_POST['lastname']) == "") || (trim($_POST['age']) == "")) 
     { 
      echo "<p>ERROR: All fields must be completed</p>"; 
      echo "<p><a href='form.php'>Return to Form</a></p>"; 
     } 
     else 
     { 
      $con=mysqli_connect("localhost","test","test","my_db"); 
      // Check connection 
      if (mysqli_connect_errno()) 
      { 
       echo "<br>Failed to connect to MySQL: " . mysqli_connect_error(); 
      } 

      // escape variables for security 
      $firstname = mysqli_real_escape_string($_POST['firstname']); 
      $lastname = mysqli_real_escape_string($_POST['lastname']); 
      $age = mysqli_real_escape_string($_POST['age']); 

      $sql="INSERT INTO Persons (FirstName, LastName, Age) 
      VALUES ('$firstname', '$lastname', '$age')"; 

      if (!mysqli_query($con,$sql)) 
      { 
       die('<br>Error: ' . mysqli_error($con)); 
      } 

      mysqli_close($con); 

      echo "<p>New item added.</p>"; 
      echo "<p><a href='form.php'>Return to Form</a></p>"; 
     } 
     ?> 
    </div> 

</body> 

感謝。

+0

[見這個答案它可以幫助你(http://stackoverflow.com/a/23166295/342740) – Prix

+0

不要在您的形式,如果發佈使用一個'action'自。 – rybo111

回答

0

恢復您form.php像下面 -

有什麼我這裏做 -

1)<form action="insert.php" method="post">
                                                                TO
        <form action="" method="post">

2)添加完所有php codeform.php

<?php 
    if ((trim($_POST['firstname']) == "") || (trim($_POST['lastname']) == "") ||  (trim($_POST['age']) == "")) 
    { 
     echo "<p>ERROR: All fields must be completed</p>"; 
     echo "<p><a href='form.php'>Return to Form</a></p>"; 
    } 
    else 
    { 
     $con=mysqli_connect("localhost","test","test","my_db"); 
     // Check connection 
     if (mysqli_connect_errno()) 
     { 
      echo "<br>Failed to connect to MySQL: " . mysqli_connect_error(); 
     } 

     // escape variables for security 
     $firstname = mysqli_real_escape_string($_POST['firstname']); 
     $lastname = mysqli_real_escape_string($_POST['lastname']); 
     $age = mysqli_real_escape_string($_POST['age']); 

     $sql="INSERT INTO Persons (FirstName, LastName, Age) 
     VALUES ('$firstname', '$lastname', '$age')"; 

     if (!mysqli_query($con,$sql)) 
     { 
      die('<br>Error: ' . mysqli_error($con)); 
     } 

     mysqli_close($con); 

     echo "<p>New item added.</p>"; 
     echo "<p><a href='form.php'>Return to Form</a></p>"; 
    } 
?> 

<html> 
<head> 
    <link rel="stylesheet" type="text/css" href="form.css"> 
    <title>MySQL Database Creation System</title> 
    <script src="jquery-1.11.0.min.js"></script> 
</head> 
<body> 
<div class="wrapper"> 
    <img src="eris.png" align="middle"> 
    <form action="" method="post"> 
    Firstname: <input type="text" size="16" name="firstname"> 
    Lastname: <input type="text" size="16" name="lastname"> 
    Age: <input type="text" size="1" maxlength="3" name="age"> 
    <input type="submit" class="button"> 
    </form> 
</div> 
</body> 
0

我希望我明白這一點。在這種情況下你就在附近。剛剛合併腳本是這樣的:

<?php 
// check if there is a post request ... 
// if not - nothing happens 
if(!empty($_POST)) 
{ 
    if ((trim($_POST['firstname']) == "") || (trim($_POST['lastname']) == "") || (trim($_POST['age']) == "")) 
    { 
     echo "<p>ERROR: All fields must be completed</p>"; 
     echo "<p><a href='form.php'>Return to Form</a></p>"; 
    } 
    else 
    { 
     // your inser code 
     $con=mysqli_connect("localhost","test","test","my_db"); 
     // Check connection 
     if (mysqli_connect_errno()) 
     { 
      echo "<br>Failed to connect to MySQL: " . mysqli_connect_error(); 
     } 

     // escape variables for security 
     $firstname = mysqli_real_escape_string($_POST['firstname']); 
     $lastname = mysqli_real_escape_string($_POST['lastname']); 
     $age = mysqli_real_escape_string($_POST['age']); 

     $sql="INSERT INTO Persons (FirstName, LastName, Age) 
     VALUES ('$firstname', '$lastname', '$age')"; 

     if (!mysqli_query($con,$sql)) 
     { 
      die('<br>Error: ' . mysqli_error($con)); 
     } 

     mysqli_close($con); 

     echo "<p>New item added.</p>"; 
     echo "<p><a href='form.php'>Return to Form</a></p>"; 
    } 
} 
?> 

<html> 
<head> 
    <link rel="stylesheet" type="text/css" href="form.css"> 
    <title>MySQL Database Creation System</title> 
    <script src="jquery-1.11.0.min.js"></script> 
</head> 
<body> 
    <div class="wrapper"> 
     <img src="eris.png" align="middle"> 
     <!-- change insert.php to form.php --> 
     <form action="form.php" method="post"> 
      Firstname: <input type="text" size="16" name="firstname"> 
      Lastname: <input type="text" size="16" name="lastname"> 
      Age: <input type="text" size="1" maxlength="3" name="age"> 
     <input type="submit" class="button"> 
    </form> 
</div> 

我希望是接近你的期望。

+0

謝謝大家,我明白了。 – Deicide

0

然後你的表單頁應該這樣。

<html> 
    <head> 
     <link rel="stylesheet" type="text/css" href="form.css"> 
     <title>MySQL Database Creation System</title> 
     <script src="jquery-1.11.0.min.js"></script> 
    </head> 
    <body> 
     <div class="wrapper"> 
      <img src="eris.png" align="middle"> 
      <form action="" method="post"> 
      Firstname: <input type="text" size="16" name="firstname"> 
      Lastname: <input type="text" size="16" name="lastname"> 
      Age: <input type="text" size="1" maxlength="3" name="age"> 
      <input type="submit" class="button"> 
      </form> 
     </div> 
    </body> 
    <?php 
    if ((trim($_POST['firstname']) == "") || (trim($_POST['lastname']) == "") || (trim($_POST['age']) == "")) 
    { 
     echo "<p>ERROR: All fields must be completed</p>"; 
     echo "<p><a href='form.php'>Return to Form</a></p>"; 
    } 
    else 
    { 
     $con=mysqli_connect("localhost","test","test","my_db"); 
     // Check connection 
     if (mysqli_connect_errno()) 
     { 
      echo "<br>Failed to connect to MySQL: " . mysqli_connect_error(); 
     } 

     // escape variables for security 
     $firstname = mysqli_real_escape_string($_POST['firstname']); 
     $lastname = mysqli_real_escape_string($_POST['lastname']); 
     $age = mysqli_real_escape_string($_POST['age']); 

     $sql="INSERT INTO Persons (FirstName, LastName, Age) 
     VALUES ('$firstname', '$lastname', '$age')"; 

     if (!mysqli_query($con,$sql)) 
     { 
      die('<br>Error: ' . mysqli_error($con)); 
     } 

     mysqli_close($con); 

     echo "<p>New item added.</p>"; 
     echo "<p><a href='form.php'>Return to Form</a></p>"; 
    } 
    ?> 

感謝名單