Flumble _
Flumble _

Reputation: 37

Adding and accessing buttons IDs in a PHP-generated table

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);
?>

Upvotes: 2

Views: 695

Answers (2)

Rosh Donniet
Rosh Donniet

Reputation: 418

(Concerns Flumble_'s answer, that I can't comment because of my low rep)

Maybe the <?= ?> are the problem. Try replacing them with <?php ?>

UPDATE :

You should also never use short open tags (<? ?>) : See the answer to this question.

Also, when you write <?php $row['id'] ?>, you are not printing the value. You must write <?php echo $row['id']; ?>. The same thing applies with short open tags (but not with the <?= syntax).

Hope this helps further. I will continue reviewing your code.

UPDATE 2 :

Alright I think I got it. mysqli_fetch_array returns a row, not the entire result set. So you have to loop through the rows until mysqli_fetch_array returns NULL :

while($data = mysqli_fetch_array($result)) {

    ?>
    <tr>
        <!-- ... -->
    </tr>
    <?php
}

Upvotes: 0

mikrafizik
mikrafizik

Reputation: 349

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'];

Upvotes: 1

Related Questions