如何在PHP的下拉菜单中访问MSQL数据库中的值

时间:2021-08-11 07:39:43

I am developing a website using PHP and MYSQL. and i made a form to add categories for the blog and stored it in the table of categories. Now i want to access those categories in the drop down menu in another form of blog . Can anyone solve my problem?

我正在开发一个使用PHP和MYSQL的网站。我做了一个表单,为博客添加类别并将其存储在类别表中。现在我想在另一种形式的博客的下拉菜单中访问这些类别。有人能解决我的问题吗?

Here is the code of form:

以下是表格守则:

 <html>

  <head>
     <title>Create a Blog!</title>
  </head>

 <body>
 <?php
    include_once ('BlogClass.php');
    $j = new Blog();
    $ans = array();
    $ans = $j->DisplayCategories();
 ?>

   <form name="BlogTopic" action="BlogTopicProcess.php" method="post" onSubmit="return validateForm()">
  topic_cat :
      <select name="topic_cat">
         <?php
             for ( $i = 0; $i < count( $ans ); $i++ ) {
         ?>
         <option value="<?php echo($ans[$i]['Category']);?>"><?php echo($ans[$i]   ['Category']);?></option>
         <? php 
             }
         ?>
      </select>
      <label><strong>topic_id:</strong></label>
      <input name="topic_id" type="text"/><br>         
      <label><strong>topic_subject:</strong></label>
      <input name="topic_subject" type="text"/><br>

      <label><strong>topic_date:</strong></label>
      <input name="topic_date" type="text"/><br>

      <label><strong>topic_by:</strong></label>
      <input name="topic_by" type="text"/><br>

      <input type="checkbox" name="terms" />
      I agree to the <a href="#">terms &amp; conditions</a>  <br>                    
      <input type="submit" value="Create Topic" /><br>
 </form>     

 </body>
</html>

The function DisplayCategory()

函数DisplayCategory()

public function DisplayCategory() { 
    $connection=$this->Con->connectDb(); 
    $data=array(); 
    $sql="select * from categories"; 
    $query=mysql_query($sql); 
    $numrows=mysql_num_rows($query); 
    if ($numrows!=0) { 
        while ($a=mysql_fetch_array($query)) 
            $data[]=$a; 
    } 
    mysql_close($connection); 
    return $data; 
}

2 个解决方案

#1


2  

I dont know what values your $ans contains, but this might help you

我不知道您的$ans包含什么值,但是这可能会对您有所帮助。

<select name='topic_cat'>
   <?php foreach ( $ans as $a ) { ?>
      <option value="<?php echo $a['key']?>"> <?php echo $a['value']; ?> </option>
   <?php } ?>
</select>

#2


1  

Firstable you must delete the blank here

首先,你必须删除这里的空白

<option value="<?php echo($ans[$i]['Category']);?>"><?php echo($ans[$i]   ['Category']);?></option>

to

<option value="<?php echo($ans[$i]['Category']);?>"><?php echo($ans[$i]['Category']);?></option>

if doesnt work then must check the method DisplayCategories() what type returns and in witch format. Send us the code of this method

如果不工作,那么必须检查方法DisplayCategories()什么类型返回,并以女巫格式返回。把这个方法的代码发给我们

replace the code $data[]=$a; with this array_push($data,$a);

替换代码数据[]= $美元;用这个array_push(数据,一美元);

#1


2  

I dont know what values your $ans contains, but this might help you

我不知道您的$ans包含什么值,但是这可能会对您有所帮助。

<select name='topic_cat'>
   <?php foreach ( $ans as $a ) { ?>
      <option value="<?php echo $a['key']?>"> <?php echo $a['value']; ?> </option>
   <?php } ?>
</select>

#2


1  

Firstable you must delete the blank here

首先,你必须删除这里的空白

<option value="<?php echo($ans[$i]['Category']);?>"><?php echo($ans[$i]   ['Category']);?></option>

to

<option value="<?php echo($ans[$i]['Category']);?>"><?php echo($ans[$i]['Category']);?></option>

if doesnt work then must check the method DisplayCategories() what type returns and in witch format. Send us the code of this method

如果不工作,那么必须检查方法DisplayCategories()什么类型返回,并以女巫格式返回。把这个方法的代码发给我们

replace the code $data[]=$a; with this array_push($data,$a);

替换代码数据[]= $美元;用这个array_push(数据,一美元);