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 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号