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 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在