dongtang5776 2014-04-03 14:40
浏览 77
已采纳

Php Javascript从while循环中显示的数据库中删除文件

I have a database, in that database is a table with 2 columns:

Column 1: id
Column 2: filename

At the moment I have 3 entries in the table, each one has their own unique id (id is set to auto-increment with each entry). each separate entry has an id of:

1
2
3

I'm using a While loop in my page to display each item:

<?php 
 require_once("parse_files/connect.php");

 $query1 = "SELECT * FROM uploads";
 $result1 = mysqli_query($connection, $query1);
 while($row1 = mysqli_fetch_assoc($result1)) {
$upload_id = $row1['id'];
$upload_filename = $row1['filename'];
 ?>

     <?php echo $upload_filename?><br>
     <button onClick="remove_file();">Remove</button>

 <?php } ?>

Here I have echo'd out the filename, And next to it I have displayed a button that will allow the user to delete the file. For testing purposes I have created this small script to see what value my javascript pulls out when I click the button:

<script>
 function remove_file(){
var filename = '<?php echo $upload_id?>'
alert(filename);
 }
</script>

When I test it I place the script within the while loop, so that I can get the value of the id (Or so i hoped), but it just doesn't work. When I click the button next to the corresponding filename, I want it to show that products id, but instead it shows me the last entries (3) no matter which button I click. I might be taking the complete wrong direction with this, could someone please point me in the right direction?

  • 写回答

2条回答 默认 最新

  • duancan8382 2014-04-03 14:44
    关注

    Try this:

    <button onClick="remove_file(<?php echo $upload_id;?>);">Remove</button>
    

    Then in javascript:

    function remove_file(id){
      alert(id);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集