将数据库项添加到php表单中


adding database item to a php form

我是php和mysql的新手,我想了解如何将数据从数据库字段传输到php表单。我在一个表上显示了数据,在最后一列上,我有一个按钮(赞助商),您可以在其中选择该行,一个特定的字段将显示在一个表单上,一旦您选择按钮(赞助者),该表单就会打开。我有三个显示数据的脚本(needed.php),其中按钮发送所选的id/No字段(add.php),下面的表单是要发送数据的表单(details.php)。请帮助了解如何进行。

      needy.php
<?php
$username = "egesachi_baby";
$password = "babyclass" ;
$database = "egesachi_babyclass";
$server = "localhost";
$db_handle = mysql_connect($server,$username,$password);
$db_found = mysql_select_db($database)or die ("cannot connect");

if ($db_found) {
$sql = "SELECT * FROM needy";
$result= mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Photo</th>
<th>Name</th>
<th>Age</th>
</tr>";
while($db_field = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . '<img src="data:image/jpeg;base64,' . 
base64_encode(     $db_field['Photo'])  .   
'"      width="280" height="280" />'; "</td>";
echo "<td>" . $db_field['Name']."</td>";
echo "<td>" . $db_field['Age']."</td>";
echo "<td>";
echo "<form action='add.php' method='post'>";
echo "<input type='hidden' name='pupilno' value='".$db_field['No']."'>";
echo "<input type='submit' value='sponsor' />";
echo "</form>";
echo "</td>";
echo "</tr>";  
}
echo "</table>";
 mysql_close($db_handle);
}

else {
   print "database not found";
 mysql_close($db_handle);
}
?>
 add.php
 <?php
     session_start();
   $username = "egesachi_baby";
   $password = "babyclass" ;
   $database = "egesachi_babyclass";
   $server = "localhost";
  $db_handle = mysql_connect($server,$username,$password);
  $db_found = mysql_select_db($database)or die ("cannot connect");

if ($db_found) {
 if(isset($_POST['sponsor'])) {
   $id = $_POST['pupilno'];
     $sql = "SELECT Name FROM needy WHERE No= '$_POST['pupilno']'";
     $result= mysql_query($sql);
 while ( $db_field = mysql_fetch_assoc($result) ) {
          $sponsored = $db_field['Name'] . "<BR>";

 }
mysql_close($db_handle);
 }
 else {
  print "Database NOT Found ";
  mysql_close($db_handle);
  }


 }

?> 
 details.php
 <html>
 <body>
      <form action="paypal.php" method="post">
      Pupil No:   <input type="text" name="pupilid"><p/>
      pupil Name: <input type="text" name="sponsored"><p/>
      Your Name:  <input type="text" name="name"><p/>
      Your Email :<input type="text" name="email"><p/>
      Phone No:   <input type="text" name="phone"><p/r>
      Country:    <input type="text" name="country"><p/>
      Contribution:<input type="text" name="con"><p/>

    <input type="submit" value="Send it!">
    </form>
</body>
</html> 

试试这个:

add.php
...
 while ( $db_field = mysql_fetch_assoc($result) ) {
          $sponsored = $db_field['Name'];

 }
header('Location: add.php?pupil='.$_POST['pupilno'].'&name='.$sponsored );
...

然后

details.php
...
  <form action="paypal.php" method="post">
  Pupil No:   <input type="text" name="pupilid" value="<?php $_GET['pupil'] ?>"><p/>
  pupil Name: <input type="text" name="sponsored" value="<?php $_GET['name'] ?>"><p/>
  Your Name:  <input type="text" name="name"><p/>
  Your Email :<input type="text" name="email"><p/>
  Phone No:   <input type="text" name="phone"><p/r>
  Country:    <input type="text" name="country"><p/>
  Contribution:<input type="text" name="con"><p/>

<input type="submit" value="Send it!">
</form>
...

然而,要验证POST/GET变量,并保护自己免受add.php页面上SQL注入的影响,还有很多工作要做——我会将add.php和details.php脚本结合起来,但这是基于您的示例的简单化视图,将它们分开可能还有其他原因。

php-mysql函数也被弃用(http://www.php.net/mysql_query)现在考虑将脚本更新为mysqli_query或PDO,并阅读SQL注入预防:如何在PHP中防止SQL注入?