duanchi0883649 2015-12-03 07:43
浏览 30
已采纳

不要从选择的mysql数据库中检索选定的值

I was trying to get selected value selected from database, but it does not work for some reason. I do not know what do I need to change, I am using function to store articles in database, and I am using the same function to get article that is selected in database. Function artikli:

function article() {
  $link = new mysqli("localhost", "xxx", "xxx", "xxx");
  $link->set_charset("utf8");
  $sql=mysqli_query($link, "SELECT * FROM `artikli` ORDER BY `Id` ASC ");
  echo '<option value="">Select article: </option>';
  while($record=mysqli_fetch_array($sql)){
  echo '<option value= "' .$record['Id']. '">' . $record['ArtCode'] ."-|-". $record['ArtName'] . ' </option>';
} 
}

In insert form this function works and insert an article. Now, in edit form, I want to get an article that is inserted. First I retrieve article from another table and I get value.

 $article = $r['ArtPalArticle'];

I get for ex. value 18. All values are integers.

$article = 18;

<div class="col-xs-12">
  <label>Article</label>
  <select class="form-control" name="article" value = "<?php echo $article; selected?>">
    <option value=<?php echo $article?> selected></option>
    <?php article(); ?>
  </select>                                       
</div> 

Could anyone give me hint, do I need to change function or I missed the code below function. It would be better for me to keep function same and change code below in select section

  • 写回答

2条回答 默认 最新

  • doulu1968 2015-12-03 07:53
    关注

    You need to compare the id with inserted id in the function. You can try this -

    function article($articleId = null) {
      $link = new mysqli("localhost", "xxx", "xxx", "xxx");
      $link->set_charset("utf8");
      $sql=mysqli_query($link, "SELECT * FROM `artikli` ORDER BY `Id` ASC ");
      echo '<option value="">Select article: </option>';
      while($record=mysqli_fetch_array($sql)){
          echo '<option value= "' .$record['Id']. '" ' . ((!empty($articleId) && $articleId == $record['Id']) ? 'selected' : '' ) . '>' . $record['ArtCode'] ."-|-".     $record['ArtName'] . ' </option>';
      } 
    }
    

    And HTML

    <div class="col-xs-12">
      <label>Article</label>
      <select class="form-control" name="article">
        <?php 
        if(!empty($article)) { // at the time of edit
           article($article); 
        } else {
           article();
        }
        ?>
      </select>                                       
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?