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

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

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

  • 写回答

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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用