在PHP生成的表格中添加和访问按钮ID [英] Adding and accessing buttons IDs in a PHP-generated table

查看:72
本文介绍了在PHP生成的表格中添加和访问按钮ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的php有问题,它会生成一个表,从SQL数据库请求数据并将数据存储在表中。



每行的第一个单元格该表包含一个下拉按钮,该按钮链接到删除该行的delete.php脚本。它还链接到一个用于修改行的单元格的modif.php脚本。
我的问题是,我需要访问带ID的下拉按钮,以知道要删除哪一行。



而且我真的不知道如何链接我的下拉按钮与ID和访问它们在我的脚本。



这是代码:

 <?php 
$ con = mysqli_connect(localhost,root,icare,icare1);
//检查连接
if(mysqli_connect_errno())
{
echo无法连接到MySQL:。 mysqli_connect_error();
}

$ result = mysqli_query($ con,SELECT * FROM magasin);

echo'< table border ='1'>
< tr>
th code< th>
th ip<< th>第b个地址第b个地址第b个地址第b个地址第b个地址第b个地址第b个地址第b个地址第b个地址第b个地址第b个地址第;第b个电子邮件第b个电子邮件第b个电子邮件第b个电子邮件第b个电子邮件第b个电子邮件第b个电子邮件第b个电子邮件第b个电子邮件第b个电子邮件第b个电子邮件第b个电子邮件第b个电子邮件第; th> date< / th>
< / tr>;

$ indexB = array();
$ i = 0;
while($ row = mysqli_fetch_array($ result))
{
echo< tr>;
echo< td>
< div class ='dropdown>
< button id = $ indexB [$ i] class ='dropbtn'>▶<按钮>

这里是delete.php:

 <?php 
$ connection = mysqli_connect(localhost,root,icare,icare1);
if($ connection === false){
die(Connection failed。mysqli_connect_error());
};


// $ id =
$ sql =DELETE FROM magasin WHERE Code =。$ id;

// $ result = mysqli_query($ connection,$ sql);

if(mysqli_query($ connection,$ sql)){
echoDone!;
} else {
echo失败:$ sql。。 mysqli_error($连接);
}
mysqli_close($ connection);
?>

我开始使用indexB []来存储下拉ID,但我不确定我做对了。
最后,我想将按钮链接到代码属性,然后使用代码属性删除与我的SQL查询的行。



我是新的对此...抱歉,如果我做了或要求一些简单的愚蠢。



更新:
到mikrafizik:



我试了你的答案,但它不起作用。我只能得到1> Supprimer,看起来href有问题,但我找不到原因。
我不知道我忘记了什么,所以如果你看到错误:

 <?php 
$ con = mysqli_connect(localhost,root,icare,icare1) ;
//检查连接
if(mysqli_connect_errno())
{
echo无法连接到MySQL:。mysqli_connect_error();
}

$ result = mysqli_query($ con,SELECT * FROM magasin);

echo< table border ='1'>
< tr>
代码< / th>
Adresse IP< / th>
Adresse ADS< / th>
Région< / th>
< th> Adresse< / th>
Nom du directeur< / th>
< th> Mail< / th>
Téléphone< / th>
GTC< / th>
< th>日期安装< / th>
< / tr>;

$ data = mysqli_fetch_array($ result);
?>

< table>
<?php foreach($ data as $ key => $ row):?>
< tr>
< td>
< div class ='dropdown-内容'>
< button class ='dropbtn'>▶< / button>
<! - < a href =modif.php?id =<?= $ row ['id']?>>修饰符< / a> - >
< a href =delete.php?id =<?php echo $ row ['id']? >> Supprimer< / a>
< / div>
< / td>
< td>< div><?php echo $ row ['AdresseIP '];?>< / div>< / td>
< td>< div><?php echo $ row ['AdresseADS'];?>< / div><< ; / td>
< td>< div><?php echo $ row ['Region'];?>< / div>< / td>
< td> < div><?php echo $ row ['Adresse'];?>< / div>< / td>
< td>< div><?php echo $ row [ 'NomDirecteur'];?>< / DIV> ;< / td>
< td>< div><?php echo $ row ['Mail'];?>< / div>< / td>
< td>< div><?php echo $ row ['Tel'];?>< / div>< / td>
< td>< div><?php echo $ row ['Gtc'];?>< / div>< / td>
< td>< div><?php echo $ row ['DateInstall'];?>< / td>
< / tr>
<?php endforeach; ?>
< / table>

<?mysqli_close($ con);?>

delete.php:

 <?php 
$ connection = mysqli_connect(localhost,root,icare,icare1);
if($ connection === false){
die(Connexionéchouée。mysqli_connect_error());
};

$ id = $ _GET ['id'];
$ sql =DELETE FROM magasin WHERE Code =。$ id;

$ result = mysqli_query($ connection,$ sql);

if($ result){
echoEnregistrementréussi!;
} else {
echoEnregistrementéchoué:$ sql。。 mysqli_error($连接);
}
mysqli_close($ connection);
?>


解决方案

首先, / p>

  $ data = mysqli_fetch_array($ result)

然后

 <?php foreach($ data as $ key => $行):?> 
< tr>
< td>
< div class ='dropdown-content'>
< a href ='modif.php?id =<?= $ row ['id']?>'>修饰符< / a>
< a href ='delete.php?id =<?= $ row ['id']?>'> Supprimer< / a>
< / div>
< / td>
< / tr>
<?php endforeach?>

并在您的modif.php中

  $ id = $ _GET ['id']; 


I have problems with my php that generates a table, requests data from a SQL database, and stores data in the table.

The first cell of each row in the table contains a dropdown button which links to a delete.php script that deletes the row. It also links to a modif.php script used to modify the row's cells. My problem is that i need to access the dropdown buttons with IDs to know which row to delete.

And i don't really know how to link my dropdown buttons with IDs and access them in my scripts.

Here's the code :

<?php
  $con=mysqli_connect("localhost","root","icare","icare1");
  // Check connection
  if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $result = mysqli_query($con,"SELECT * FROM magasin");

  echo "<table border='1'>
  <tr>
  <th>code</th>
  <th>ip</th>
  <th>ads</th>
  <th>region</th>
  <th>adress</th>
  <th>name</th>
  <th>email</th>
  <th>number</th>
  <th>gtc</th>
  <th>date</th>
  </tr>";

  $indexB = array();
  $i = 0;
  while($row = mysqli_fetch_array($result))
  {
    echo "<tr>";
    echo "<td>
          <div class='dropdown'>
            <button id=$indexB[$i] class='dropbtn'>▶</button>
            <div class='dropdown-content'>
              <a href='modif.php'>Modifier</a>
              <a href='delete.php'>Supprimer</a>
            </div>
            ".$row['code']."
          </div>
    </td>";

    echo "<td><div>" . $row['ip'] . "</div></td>";
    echo "<td><div>" . $row['ads'] . "</div></td>";
    echo "<td><div>" . $row['region'] . "</div></td>";
    echo "<td><div>" . $row['adress'] . "</div></td>";
    echo "<td><div>" . $row['name'] . "</div></td>";
    echo "<td><div>" . $row['email'] . "</div></td>";
    echo "<td><div>" . $row['number'] . "</div></td>";
    echo "<td><div>" . $row['gtc'] . "</div></td>";
    echo "<td><div>" . $row['date'] . "</td>";
    echo "</tr>";
    $i++;
  }
    echo "</table>";
    mysqli_close($con);
?>

And here is the delete.php :

<?php
  $connection = mysqli_connect("localhost", "root", "icare", "icare1");
  if($connection === false){
      die("Connection failed " . mysqli_connect_error());
  };


    //$id =
    $sql = "DELETE FROM magasin WHERE Code=".$id;

    //$result = mysqli_query($connection,$sql);

    if(mysqli_query($connection, $sql)){
        echo "Done !";
    } else{
      echo "Failed : $sql. " . mysqli_error($connection);
    }
  mysqli_close($connection);
?>

I started an indexB[] to store the dropdowns IDs but i'm not sure that i'm doing it right. In the end I want to link my buttons to the code attribute and then delete the row with my sql query using the code attribute.

I'm new to this so ... sorry if i did or ask something plain stupid.

UPDATE : To mikrafizik :

I tried your answer but it doesn't work. I only get "1">Supprimer". It seemsi have a problem with the href but i just can't find why. I don't know what i forgot, so if you see something wrong :

<?php
  $con=mysqli_connect("localhost","root","icare","icare1");
  // Check connection
  if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $result = mysqli_query($con,"SELECT * FROM magasin");

  echo "<table border='1'>
  <tr>
  <th>Code</th>
  <th>Adresse IP</th>
  <th>Adresse ADS</th>
  <th>Région</th>
  <th>Adresse</th>
  <th>Nom du directeur</th>
  <th>Mail</th>
  <th>Téléphone</th>
  <th>GTC</th>
  <th>Date d'installation</th>
  </tr>";

$data = mysqli_fetch_array($result);
?>

    <table>
<?php foreach ($data as $key => $row):?>
<tr>
 <td>
    <div class='dropdown-content'>
      <button class='dropbtn'>▶</button>
          <!-- <a href="modif.php?id=<?=$row['id']?>">Modifier</a> -->
          <a href="delete.php?id=<?php echo $row['id']?>">Supprimer</a>
    </div>
 </td>
 <td><div><?php echo $row['AdresseIP'];?></div></td>
 <td><div><?php echo $row['AdresseADS'];?></div></td>
 <td><div><?php echo $row['Region'];?></div></td>
 <td><div><?php echo $row['Adresse'];?></div></td>
 <td><div><?php echo $row['NomDirecteur'];?></div></td>
 <td><div><?php echo $row['Mail'];?></div></td>
 <td><div><?php echo $row['Tel'];?></div></td>
 <td><div><?php echo $row['Gtc'];?></div></td>
 <td><div><?php echo $row['DateInstall'];?></td>
</tr>
<?php endforeach; ?>
</table>

<?mysqli_close($con);?>

delete.php :

<?php
  $connection = mysqli_connect("localhost", "root", "icare", "icare1");
  if($connection === false){
      die("Connexion échouée " . mysqli_connect_error());
  };

    $id = $_GET['id'];
    $sql = "DELETE FROM magasin WHERE Code=".$id;

    $result = mysqli_query($connection,$sql);

    if($result){
        echo "Enregistrement réussi !";
    } else{
      echo "Enregistrement échoué : $sql. " . mysqli_error($connection);
    }
  mysqli_close($connection);
?>

解决方案

At first, divide query and form building like that

$data = mysqli_fetch_array($result)

then

<?php foreach ($data as $key => $row): ?>
<tr>
 <td>
    <div class='dropdown-content'>
          <a href='modif.php?id=<?=$row['id']?>'>Modifier</a>
          <a href='delete.php?id=<?=$row['id']?>'>Supprimer</a>
    </div>
 </td>
</tr>
<?php endforeach ?>

And in your modif.php

$id = $_GET['id'];

这篇关于在PHP生成的表格中添加和访问按钮ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆