dpx49470
2014-04-17 18:27
浏览 150
已采纳

从数据库检索数据到下拉列表

I would like to display a drop down list with data retrieved from a table with only one column

this is my form

<html>
<head>

</head>
<body>

<form action="insert_customer_complaint.php" method="post">

Name: <input type="text" name="name"><br>
Complaint: <input type="text" name="comp"><br>
Reason: <select name="reason">
Add <input type="submit">

</form>

</body>
</html>

this the php file i use to insert data

    <?php
// Create connection
$con=mysqli_connect("localhost","ccc","ccc","ccc");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO reason (reason_name)
VALUES
('$_POST[reason_name]')";


if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);

?>

Inserting part works very well I need to get data for the drop down list from a table with one column using the same php file

table name = reason, column name = reason_name

please help me out

I have manged to insert data and every thing works well. now i want to generate report in a table from where the the column name should be retrieved from from the data in a particular row here's what i need

please see click for image

http://testserverforprojects.tk/CC/tables.JPG

table should be dynamic because it should have a latest year at the end for instance this year only will have data upto 2013 so 2013 will be the last column.. so in 2015, 2014 will be the last column

图片转代码服务由CSDN问答提供 功能建议

我想显示一个下拉列表,其中包含从只有一列的表中检索到的数据 \ n

这是我的表格

 &lt; html&gt; 
&lt; head&gt; 
 
&lt; / head&gt; 
&lt; body&gt; 
 
&lt  ; form action =“insert_customer_complaint.php”method =“post”&gt; 
 
名称:&lt; input type =“text”name =“name”&gt;&lt; br&gt; 
Complaint:&lt; input type =“text  “name =”comp“&gt;&lt; br&gt; 
 Reason:&lt; select name =”reason“&gt; 
Add&lt; input type =”submit“&gt; 
 
&lt; / form&gt; 
 
&lt;  / body&gt; 
&lt; / html&gt; 
   
 
 

这个用于插入数据的php文件

 &lt  ;?php 
 //创建连接
 $ con = mysqli_connect(“localhost”,“ccc”,“ccc”,“ccc”); 
 
 //检查连接
if(mysqli_connect_errno())
  {
 echo“无法连接到MySQL:”。  mysqli_connect_error(); 
} 
 
 $ sql =“INSERT INTO reason(reason_name)
VALUES 
('$ _ POST [reason_name]')”; 
 
 
if(!mysqli_query($ con,$)  sql))
 {
 die('错误:'。mysqli_error($ con)); 
} 
echo“添加了1条记录”; 
 
mysqli_close($ con); 
 
?n&nbsp; \  n   
 
 

插入部分非常有效我需要使用相同的php文件从一个表中获取下拉列表的数据 \ n

表名=原因,列名= reason_name

请帮帮我

我已经插入数据并且每件事情都运作良好 。 现在我想在一个表格中生成报告,从中应该从特定行的数据中检索列名称这里是我需要的

请参阅点击图片

http://testserverforprojects.tk/CC/tables.JPG < / p>

表应该是动态的,因为它应该在最后一年结束,例如今年只有2013年的数据,所以2013年将是最后一列..所以在2015年,2014年将是 最后一栏

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douwei7471 2014-04-17 18:42
    已采纳

    Use this:

        <html>
    <head>
    
    </head>
    <body>
    
    <form action="insert_customer_complaint.php" method="post">
    
    Name: <input type="text" name="name"><br>
    Complaint: <input type="text" name="comp"><br>
    Reason: <select name="reason">
        <?php
        $con=mysqli_connect("localhost","ccc","ccc","ccc");
        if (mysqli_connect_errno())
        {echo "Failed to connect to MySQL: " . mysqli_connect_error();}
        $sql="SELECT * FROM reason";
        $result = mysqli_query($con, $sql);
        while($row = mysqli_fetch_array($result))
      {    
        echo '<option value='.$row['reason_name'].'>'.$row['reason_name'].'</option>';
          }
    ?>
        </select>
    Add <input type="submit">
    
    </form>
    
    </body>
    </html>
    
    打赏 评论
  • duanjian7617 2014-04-17 18:30

    Wouldn't it just be :

    $sql="SELECT reason_name From reason";
    

    ? Then iterate over the returned set and build the related html to accomplish your interface requirements

    打赏 评论

相关推荐 更多相似问题