2014-02-17 33 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"> 
相关问题