【问题标题】:php mysql forms pass results from database to orignal form fieldphp mysql 表单将结果从数据库传递到原始表单字段
【发布时间】:2014-02-17 18:15:08
【问题描述】:

我有一个问题,我的所有方法都有效,但我终其一生都无法弄清楚如何将 sql 查询的结果回显到每条查询信息的原始表单字段行。下面是我的代码示例。

附:我尝试使用一个变量来回显 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>';
?>

【问题讨论】:

    标签: php mysql sql database forms


    【解决方案1】:

    我假设您将表单提交到同一页面,这很好。如果是这样,首先声明您的变量并将您的 sql 移动到您的表单上方。然后对于每个表单域,执行以下操作:

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

    在您的 html 中,将每个变量放入关联表单元素的值中,以便在表单已提交时显示,如果未提交则显示为空。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 2012-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多