-1

I'm trying to connect my website to the MySQL table.

Here is my code;

<?php
$v1 = mysql_query('SELECT * FROM ('.$nompage.') ORDER BY id');
if($v1 === FALSE) {
    die('Erreur lors de la selection de la table.'); // TODO: meilleur erreur
}
while($liste = mysql_fetch_array($v1)){
?>

<tr>
<th><?php if(isset($liste['nom'])){
  echo $liste["nom"];} ?></th>
<th><a href="<?php echo $liste['lien1']; ?>"><i class="fa fa-external-link"></i>
<?php echo $liste['lien1']; ?></a></th>
<th><?php echo $liste['autre']?></th>
</tr>
<?php
}
?>

The $nompage fonction is ->

$nompage = htmlspecialchars($_GET["nom"]);
$nompage = mysql_real_escape_string($nompage);

I can see 4 row, because I have 4 items in my tables.

This is the error I get on the second and third column.

Notice: Undefined index: lien1 in C:\xampp\htdocs\show.php on line 102
Notice: Undefined index: autre in C:\xampp\htdocs\show.php on line 103
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Félix Desjardins
  • 3,223
  • 3
  • 21
  • 36

3 Answers3

0

The query should be -

'SELECT * FROM '.$nompage.' ORDER BY id' // if $nompage contains the table name

And avoid using mysql. It is deprecated. Try to use mysqli or PDO.

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
0

This might be annoying for you, but you should use PDO instead of MySQL. MySQL syntax is deprecated. Anyhow, you may try this:

<?php
    //Pretty sure you need to give it either ASC or DESC.
    $v1 = mysql_query('SELECT * FROM "'.$nonpage.'" ORDER BY id DESC');
    if($v1 === FALSE) {
        die('Erreur lors de la selection de la table.'); // TODO: meilleur erreur
    }
        while($liste = mysql_fetch_array($v1)){
    ?>
<tr>
    <th><?php if(isset($liste['nom'])){
    echo $liste["nom"];} ?></th>
    <th><a href="<?php echo $liste['lien1']; ?>"><i class="fa fa-external-link"></i>
    <?php echo $liste['lien1']; ?></a></th>
    <th><?php echo $liste['autre']?></th>
</tr>
<?php
}
?>
user2879055
  • 176
  • 9
0
<?php
$query= 'SELECT * FROM "'.$nonpage.'" ORDER BY id ASC'
$v1 = mysql_query($query);
if($v1 === FALSE) {
    die('Erreur lors de la selection de la table.'); 
}
while($liste = mysql_fetch_assoc($v1)){
?>

<tr>
<th><?php if(isset($liste['nom'])){
  echo $liste["nom"];} ?></th>
<th><a href="<?php echo $liste['lien1']; ?>"><i class="fa fa-external-link"></i>
<?php echo $liste['lien1']; ?></a></th>
<th><?php echo $liste['autre']?></th>
</tr>
<?php
}
?>

use mysql_fetch_assoc insted of mysql_fetch_array

Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20