2014-02-17 63 views
0

我有一個問題,我的所有方法都可以工作,但我無法爲我的生活弄清楚如何將sql查詢的結果回顯到原始表單域行一塊查詢信息。以下是我的代碼示例。php mysql表單將結果從數據庫傳遞給orignal表單域

P.S.我嘗試使用變量來保存相關數據來回顯sql代碼底部的原始表,但它只是在頁面底部創建一個重複的表格表。看代碼

<html> 
<title> Student Main Record:</title> 
<center><b><font size="15"><u>Student Main Record</center></font></b><br><BR></u> 
<form method="POST" action="test.php"> 
<table align="center" width="90%" border="0"> 
<tr> 
<td> 
<table width="100%" border="0"><center><font size="6"><b>Student Information:</center</table></font>     
<table border="0"> 
    <tr><td>Panther ID</td><td><input type="text" value="" size="50" maxlength="7" name="PID"></td></tr>       
    <tr><td>First Name</td><td><input type="text" value="" size="50" maxlength="50" name="firstName"></td></tr> 
    <tr><td>Middle Name</td><td><input type="text" value="" size="50" maxlength="50" name="middleName"></td></tr> 
    <tr><td>Last Name</td><td><input type="text" value="" size="50" maxlength="50" name="lastName"></td></tr> 
     <tr><td>Age</td><td><input type="text" value="" size="50" maxlength="3" name="age"></td></tr> 
<tr><td>Gender</td><td><input type="radio" checked name="Gender" value="Female">Female <input type="radio" name="Gender" value="Male">Male</td></tr> 
<tr><td>Country</td><td><input type="text" value="" size="50" maxlength="30" name="country"></td></tr>    
    </td></tr></tr></tr> 
    <tr></tr> 
    <tr></tr> 
     <tr></tr>       
     <tr><td>Address1</td><td><input type="text" value="" size="50" maxlength="50" name="add1"></td></tr> 
    <tr><td>Address2</td><td><input type="text" value="" size="50" maxlength="50" name="add2"></td></tr> 
    <tr><td>City</td><td><input type="text" value="" size="50" maxlength="50" name="city"></td></tr> 
    <tr><td>State</td><td><input type="text" value="" size="50" maxlength="50" name="state"></td></tr> 
    <tr><td>Zip Code</td><td><input type="text" value="" size="50" maxlength="10" name="zip"></td></tr> 
    <tr><td>Primary Tel #</td><td><input type="text" value="" size="50" maxlength="20" name="tel1"></td></tr> 
    <tr><td>Secondary Tel #</td><td><input type="text" value="" size="50" maxlength="20" name="tel2"></td></tr> 
    <tr><td>Email</td><td><input type="text" value="" size="50" maxlength="50" name="email"></td></tr>      
</table> 
</td> 
    <td> 
<table width="100%" border="0"> 
      </table>      
    <table border="0"><br><br> 
    <tr><font size=5pt><center>&nbsp&nbsp&nbsp&nbspEmergency Contact Information:</tr></font></center><tr><tr><tr><tr><Tr><tr></tr></tr></tr> 
    <tr><td>First Name</td><td><input type="text" value="" size="50" maxlength="50" name="ECfirstName"></td></tr> 
    <tr><td>Middle Name</td><td><input type="text" value="" size="50" maxlength="50" name="ECmiddleName"></td></tr> 
     <tr><td>Last Name</td><td><input type="text" value="" size="50" maxlength="50" name="EClastName"></td></tr> 
    <tr><td>Age</td><td><input type="text" value="" size="50" maxlength="3" name="ECage"></td></tr> 
<tr><td>Gender</td><td><input type="radio" checked name="Gender1" value="Female">Female <input type="radio" name="Gender1" value="Male">Male</td></tr> 
<!-- <tr><td>Social Security</td><td><input type="text" size="50" maxlength="10" name="ECss">--> 
<tr><td>Country</td><td><input type="text" value="" size="50" maxlength="30" name="ECcountry"></td></tr> 
</td></tr></tr> 
    </tr> 
     <tr></tr> 
    <tr></tr> 
     <tr></tr>       
     <tr><td>Address1</td><td><input type="text" value="" size="50" maxlength="50" name="ECadd1"></td></tr> 
    <tr><td>Address2</td><td><input type="text" value="" size="50" maxlength="50" name="ECadd2"></td></tr> 
    <tr><td>City</td><td><input type="text" value="" size="50" maxlength="50" name="ECcity"></td></tr> 
    <tr><td>State</td><td><input type="text" value="" size="50" maxlength="50" name="ECstate"></td></tr> 
    <tr><td>Zip Code</td><td><input type="text" value="" size="50" maxlength="10" name="ECzipCode"></td></tr> 
    <tr><td>Primary Tel #</td><td><input type="text" value="" size="50" maxlength="20" name="ECtel1"></td></tr> 
    <tr><td>Secondary Tel #</td><td><input type="text" value="" size="50" maxlength="20" name="ECtel2"></td></tr> 
    <tr><td>Email</td><td><input type="text" value="" size="50" maxlength="50" name="ECemail"></td></tr>      
</table> 
</td> 
</tr> 
</table> 
<tr> 
<td> 
</td> 
</tr> 
<tr> 
<td> 
</td> 
</tr> 
<table width="100%" border="0"> 
<tr> 
    <tr><br> 
<td align="center"> 
<input type="submit" name="find" value="Find"> 
<input type="submit" name="save" value="Save"> 
<input type="submit" name="modify" value="Modify"> 
<input type="submit" name="delete" value="Delete"> 
</td> 
</tr> 
</tr> 

<tr> 
<td> 
</td> 
</tr> 
</table> 

下面的PHP代碼 - 每個方法的作品,但我想回顯他們填充上面的原始表單字段。

if (!empty($_POST['save'])) 
{ 

$connection = mysqli_connect("xxxxxxx","xxxxxxx","xxxxxxxx"); 
    // Check connection 
     if (!$connection) 
    { 
     echo "Connection failed: " . mysqli_connect_error(); 
    } 
     else 

     //select a database 
      $dbName="spr14_aglic006"; 
      $db_selected = mysqli_select_db($connection,$dbName); 
      //confirm connection to database 
      if (!$db_selected) 
      { 
die ('Can\'t use foo : ' . mysqli_error()); 
      } 
      mysqli_query($connection,"INSERT INTO students 
      (StudentID, 
       FirstName, 
       LastName, 
       MiddleName, 
       Address1, 
       Address2, 
       City, 
       State, 
       Country, 
       Zip, 
       Telephone1, 
       Telephone2, 
       ECFirstName, 
       ECLastName, 
       ECMiddleName, 
       ECAddress1, 
       ECAddress2, 
       ECCity, 
       ECState, 
       ECCountry, 
       ECZip, 
       ECTelephone1, 
       ECTelephone2) 
      VALUES 
      ('".$_POST['PID']."', 
      '".$_POST['firstName']."', 
      '".$_POST['lastName']."', 
      '".$_POST['middleName']."', 
      '".$_POST['add1']."', 
      '".$_POST['add2']."', 
      '".$_POST['city']."', 
      '".$_POST['state']."', 
      '".$_POST['country']."', 
      '".$_POST['zip']."', 
      '".$_POST['tel1']."', 
      '".$_POST['tel2']."', 
      '".$_POST['ECfirstName']."', 
      '".$_POST['EClastName']."', 
      '".$_POST['ECmiddleName']."', 
      '".$_POST['ECadd1']."', 
      '".$_POST['ECadd2']."', 
      '".$_POST['ECcity']."', 
      '".$_POST['ECstate']."', 
      '".$_POST['ECcountry']."', 
      '".$_POST['ECzipCode']."', 
      '".$_POST['ECtel1']."', 
      '".$_POST['ECtel2']."')"); 
      echo "Record created."; 
      mysqli_close($connection); 
} 





if (!empty($_POST['find'])) 
{ 

$connection = mysqli_connect("xxxxxxx","xxxxxxx","xxxxxxxx"); 
// Check connection 
     if (!$connection) 
    { 
     echo "Connection failed: " . mysqli_connect_error(); 
    } 

      $dbName="spr14_aglic006"; 
      mysqli_select_db($connection,$dbName) or die(mysqli_error()); 


$result = mysqli_query($connection,"SELECT * FROM students WHERE StudentID='".$_POST['PID']."'"); 
if (!$result) 
{echo"No record found" . mysqli_error($connection);} 


    while($row = mysqli_fetch_array($result)) 
      { 
       echo $row['StudentID'] . " " . $row['FirstName'] . " " . $row['LastName'] . " " . 
        $row['MiddleName'] . " " . $row['Address1'] . " " . $row['Address2'] . " " . 
        $row['City'] . " " . $row['State'] . " " . $row['Country'] . " " . 
        $row['Zip'] . " " . $row['Telephone1'] . " " . $row['Telephone2'] . " " . 
        $row['ECFirstName'] . " " . $row['ECLastName'] . " " . $row['ECMiddleName'] . " " . 
        $row['ECAddress1'] . " " . $row['ECAddress2'] . " " . $row['ECCity'] . " " . 
        $row['ECState'] . " " . $row['ECCountry'] . " " . $row['ECZip'] . " " . 
        $row['ECTelephone1'] . " " . $row['ECTelephone2']; 
       mysqli_close($connection); 
      } 

} 


if (!empty($_POST['modify'])) 
{ 
$connection = mysqli_connect("xxxxxxx","xxxxxxx","xxxxxxxx"); 
    // Check connection 
     if (!$connection) 
    { 
     echo "Connection failed: " . mysqli_connect_error(); 
    } 
     else 
     { 
      //select table 
      $dbName="spr14_aglic006"; 
      mysqli_select_db($connection,$dbName) or die(mysqli_error()); 
      echo "Database Found! <br>"; 

      $query =("UPDATE students 
SET FirstName='".$_POST['firstName']."', LastName='".$_POST['lastName']."' 
WHERE StudentID='".$_POST['PID']."'"); 

      $res = mysqli_query($connection,$query); 

      if ($res) 
      { 
       echo "<p>Record Updated<p>"; 
      } 
      else 
      { 
       echo "Problem updating record. MySQL Error: " . mysqli_error($connection); 
      } 
     } 
     mysqli_close($connection); 
} 






if (!empty($_POST['delete'])) 
{ 

$connection = mysqli_connect("xxxxxxx","xxxxxxx","xxxxxxxx"); 

     // Check connection 
     if (!$connection) 
    { 
     echo "Connection failed: " . mysqli_connect_error(); 
    } 
     else 
     { 
      //select table 
      $dbName="spr14_aglic006"; 
      mysqli_select_db($connection,$dbName) or die(mysqli_error()); 
      echo "Database Found! <br>";  
$sql =("DELETE FROM students WHERE StudentID='".$_POST['PID']."'"); 

      $retval = mysqli_query($connection,$sql); 
      if($retval) 
      { 
       echo "Deleted data successfully\n"; 
      } 
      else 
      echo "Record not found";   
      } 
mysqli_close($connection); 
} 
echo '<center><b><font size="15"><u>Student Main Record</center></font></b><br><BR></u> 
<tr><td>Panther ID</td><td><input type="text" value=".$PID." size="50" maxlength="7"></td></tr>'; 
?> 

回答

0

我假設你要將表單提交到同一頁面,這很好。如果是這樣,首先聲明你的變量,並將你的SQL移動到你的表單上。然後對於每個表單域,請執行下列操作:

if(isset($_POST[PID]){ 
    $pid = $_POST[PID]; 
}else{ 
    $pid = ""; 
} 

在你的HTML,將每個變量相關的表單元素的值,以便它顯示如果表單已經提交或者其空如果標有未提交。

<input type="text" value="$pid" size="50" maxlength="7" name="PID"> 
相關問題