2

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);
?>
2
  • simply add unique identifiers to the link url to delete from that line. pass parameters with the link. so basically the url will look some thing like delete.php?id=1 Commented Apr 27, 2016 at 9:24
  • Thanks, i read about passing parameters in url but didn't know how to do it. Commented Apr 27, 2016 at 9:40

2 Answers 2

1

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'];
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot ! I'll try that ! Dividing like this make more sense.
Well this seems to be better but i can't get it to work. I made an answer about it, if you can take a look at it ... Well, thank you.
0

(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
}

6 Comments

Sure it is better, but doesn't fix the problem :/
Thanks a lot. I'll try to change the <? ?> !
I updated the code, but this still does not work. Thank you taking the time to review :)
so you have an error?? what type of err? maybe you haven't id index?
"Illegal string offset" everywhere It happens on every iteration of the loop on each 'echo' Also the undefide index error
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.