将数据库项添加到php表单

时间:2013-12-07 10:39:15

标签: php mysql forms

我是php和mysql的新手,我想要一些关于如何将数据从数据库字段传输到php表单的帮助。我已经在表格上显示了数据,在最后一列我有一个按钮(赞助商)您可以选择该行,并且一旦您选择按钮(赞助商),将打开一个特定字段.i有三个显示数据的脚本(needy.php),其中按钮发送id / No字段选择(add.php)并在下面发送数据的形式(details.php)是code.please如何进行操作的帮助。

      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> 

1 个答案:

答案 0 :(得分:1)

试试这个:

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变量,并保护自己免受SQL注入add.php页面 - 我会结合add.php和details.php脚本,但这是基于关于你的例子的简单化观点,可能还有其他原因让他们分开。

php mysql函数也已被弃用(http://www.php.net/mysql_query)看看现在将脚本更新到mysqli_query或PDO并阅读SQL注入预防:How can I prevent SQL injection in PHP?