0

我有一个包含多个字段的多行数据库。

我有一个包含下拉列表的表单。下拉列表显示数据库中每一行的一个数据库字段 (field_name)。

当用户选择所需的条目并点击 SUBMIT 时,该值将传递到 results.php 页面并可以通过 $_POST 使用。

所有这些目前都有效。

我想要一种方法来发送与从数据库中选择的字段(不仅仅是“field_name”)的行相对应的行的其余字段以及从下拉菜单中选择的内容。

例如,如果我有一个数据库,其中的行包含名为“name”、“date”和“age”的字段,我希望所有数据库行“name”出现在下拉列表中,并且一旦提交,将该特定名称的“日期”和“年龄”传递给 results.php 以在该页面上使用。

<html>
<head>
<title>Drop Down Test</title>
</head>

<body style="font-family: verdana; font-size: 11px;">

<?php

//Variables for connecting to database.
$hostname = "abcd";
$username = "abcd";
$dbname = "abcd";
$password = "abcd";
$usertable = "abcd";

//Connecting to database
$connection = mysql_connect($hostname, $username, $password) OR DIE ("Unable to connect to database!");
$db = mysql_select_db($dbname);

$query = "SELECT * FROM abcd";
$result = mysql_query($query) or die(mysql_error());

?>

<h2>Drop Down Test Form</h2>

<p>Please fill out the form below and click submit.</p>

<form action="results.php" method="POST">

    <p>Drop Down Test:
        <select name='event'>
        <!-- Drop down -->
        <?php
        while($row = mysql_fetch_array($result))
    {
        echo '<option>' . $row['field_name']. '</option>';
            }
        ?>
    </select>

    <p><input type="submit" value="Submit"><p>

</form>

4

3 回答 3

0

你应该value像这样选择你的选项:

echo '<option value = "'.$row['field_name'].'" name = "">' . $row['field_name']. '</option>';

然后你可以通过$_POST['event'];

更新

从选择中获取所有值,您可以使用$_SESSION变量将其传递给其他 php.file。

于 2013-04-05T01:11:40.187 回答
0
  //  First of all, I advice you to connect via PDO, or at least msqli, because mysql_query is depreciated.
  //  To connect with database you need:
  DEFINE("USER", "root"); 
  DEFINE("DBNAME", "test"); 
  DEFINE("DBPASSWORD", ""); 
  DEFINE("DBHOST", "localhost");
  $dbh = new PDO('mysql:host='.DBHOST.';dbname='.DBNAME,USER,DBPASSWORD,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));


   //The query:
   $sth = $dbh->prepare("SELECT name,age,date FROM test");
   $sth->execute();

  //the drop down form
    echo '<form action="results.php" method="POST">
    <select name="event"><option value=0></option>';
    while ($result = $sth->fetch(PDO::FETCH_ASSOC)) { extract($result);
    echo '<option value="date:'.$date.'-age:'.$age.'"/>'.$name.'</option>'; 
    echo '</select>
        <p><input type="submit" value="Submit"><p>
    </form>';
    }

//the event in the records.php by clicking submit
if(isset($_POST['event'])){
echo 'name:',$name'-date:',$date,'-$age',$age;
}
于 2013-04-05T01:12:35.490 回答
0

这成功了(在 results.php 中):

<?php

$hostname = "****";
$username = "****";
$dbname = "****";
$password = "****";
$usertable = "abcd";

$connection = mysql_connect($hostname, $username, $password) OR DIE ("Unable to connect to database!");
$db = mysql_select_db($dbname);

//it was this SQL query that was the key, namely the WHERE statement
$query = "SELECT * from abcd where field_name='$_POST[event]'"; 

$result = mysql_query($query) or die(mysql_error()); 
$row = mysql_fetch_row($result); 

echo "id: " . $row[0] . "<br/>";
echo "field_name: " . $row[1] . "<br/>";
//etc...

//try to throw the individual results into variables
$variable = $row[1];

echo "Check to see that the variable was passed a value: " . $variable . "<br />"; 
echo "Check to see that form selection carried over: " . $_POST['event'] . "<br />"; 

?>

我意识到这不是“最新”的做事方式,我现在将尝试让一切“现代化”。

感谢所有的帮助!

于 2013-04-05T17:17:58.457 回答