Home > front end >  Only the information of the first record appears (PHP,SQL)
Only the information of the first record appears (PHP,SQL)

Time:06-26

I'm working on a project that I would like to get an answer as soon as possible.

Problem: I have a project I'm doing. In this project, there will be events to buy tickets. The index will show all the records of the SQL database. When the event is clicked, only the information of the selected project will appear. However, this is where the problem lies. When I click on an event in the Index, it goes to the individual page of the product, but on that page, only the information of the first record always appears. Even if I click on another event in the index, the information from the first record will always appear on the individual page. (Example: The id 4 and 5 appear in the index. However, when I click on event 5, the information of id 4 will always appear on the page "eventoindividual.php". I would like your best attention to solve the problem.

[DATABASE SQL]

https://i.stack.imgur.com/KtoUQ.png

index.php

<?php
session_start();
    require('config.php');

if(!$con){
    header('location:erro.php');
} 

$sql=sprintf("SELECT * FROM eventos ORDER BY id DESC LIMIT 8");
$res=mysqli_query($con,$sql);
    

?>
<!DOCTYPE html>
<html>
<head>
    <link rel="shortcut icon" href="logotipoicon.png" />
    <!-- ==========================
        Meta Tags 
    =========================== -->
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <!-- ==========================
        Title 
    =========================== -->
    <title>shop</title>
        
    <!-- ==========================
        Fonts 
    =========================== -->
    <link href='https://fonts.googleapis.com/css?family=Source Sans Pro:400,200,200italic,300,300italic,400italic,600,600italic,700,700italic,900,900italic&amp;subset=latin,latin-ext' rel='stylesheet' type='text/css'>
    <link href='https://fonts.googleapis.com/css?family=Raleway:400,100,200,300,500,600,700,900,800' rel='stylesheet' type='text/css'>

    <!-- ==========================
        CSS 
    =========================== -->
    <link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link href="https://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css">
    <link href="assets/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="assets/css/dragtable.css" rel="stylesheet" type="text/css">
    <link href="assets/css/owl.carousel.css" rel="stylesheet" type="text/css">
    <link href="assets/css/animate.css" rel="stylesheet" type="text/css">
    <link href="assets/css/color-switcher.css" rel="stylesheet" type="text/css">
    <link href="assets/css/custom.css" rel="stylesheet" type="text/css">
    <link href="assets/css/color/red.css" id="main-color" rel="stylesheet" type="text/css">
    
    <!-- ==========================
        JS 
    =========================== -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    
</head>
<body>
    
    <!-- ==========================
        SCROLL TOP - START 
    =========================== -->
    <div id="scrolltop" ><i ></i></div>
    <!-- ==========================
        SCROLL TOP - END 
    =========================== -->
    
    <!-- ==========================
        COLOR SWITCHER - START 
    =========================== -->
    
    <!-- ==========================
        COLOR SWITCHER - END 
    =========================== -->
    
    <div id="page-wrapper"> <!-- PAGE - START -->
    
    <!-- ==========================
        HEADER - START 
    =========================== -->
    <div >
        <div >
            <div >
                <div >
                    <ul >
                        <li><i ></i> [email protected]</li>
                        <li><i ></i>  1111111111</li>
                    </ul>
                </div>
                <div >
                    <ul >
                        <li><a href="my-account.html">Minha Conta</a></li>
                        <li><a href="checkout.html">Checkout</a></li>
                        <li><a href="wishlist.html">Lista de Desejos</a></li>
                        <li><a href="compare.html">Comparar</a></li>
                        <li><a href="signin.html">Logout</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
        
    <header >
        <div  style="width: 50%;">
            <div >
                <a href="index.php" > <img src="logotipo.png"> </a>
                <button type="button"  data-toggle="collapse" data-target=".navbar-collapse"> <i ></i> </button>
            </div>
                
               <!-- <div ><img src="opcoes/teatroarte.png"></div>
                    <div ><img src="opcoes/musicafestivais.png"></div>
                        <div ><img src="opcoes/familia.png"></div>
                            <div ><img src="opcoes/desportoaventura.png"></div> -->
                
            <div >
                <ul >
                    <li >
                    </li>
                   
                    <li >
                        <a href="carrinho.php"  data-toggle="dropdown" data-hover="dropdown" data-delay="300" data-close-others="true"><i ></i></a>
                        <ul >
                            
                        
                            <li>
                                <div >
                                    <div >
                                        <a href="carrinho.php" >Ver carrinho</a>
                                    </div>
                                    <div >
                                        <a href="checkout.html" >Finalizar</a>
                                    </div>
                                </div>
                            </li>
                            <!-- CART ITEM - END -->
                            
                        </ul>
                    </li>
                    <li >
                        <a href="#"  data-toggle="dropdown"><i ></i></a>
                        <ul >
                            <li>
                                <form>
                                    <div >
                                        <input type="text"  placeholder="Pesquisar...">
                                        <span >
                                            <button  type="button">Pesquisar</button>
                                        </span>
                                    </div>
                                </form>
                            </li>
                        </ul>
                    </li>
                    <li >
                        <a href="signin.php"  data-toggle="dropdown" data-hover="dropdown" data-delay="300" data-close-others="true"></a>
                        <ul >
                            <li><a href="signin.php">Iniciar Sessão</a></li>
                            <li><a href="signup.php">Criar Conta</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
            </header>
    <!-- ==========================
        HEADER - END 
    =========================== -->  
           
    <!-- ==========================
        JUMBOTRON - START 
    =========================== -->
    <section >
        <div id="homepage-2-carousel" >
            
            <div >
                <div ></div>
                <div >
                </div>
            </div>
            <div >
                <div ></div>
                <div >
                </div>
            </div>
            <div >
                <div ></div>
                <div >
                </div>
            </div>
            <div >
                <div ></div>
                <div >
                </div>
            </div>
            <div >
                <div ></div>
                <div >
                </div>
            </div>
            <div >
                <div ></div>
                <div >
                </div>
            </div>
        </div>
    </section>
    <!-- ==========================
        JUMBOTRON - END 
    =========================== --> 
                
            </div>
    
    <!-- ==========================
        RECENT BLOG POSTS - START 
    =========================== -->

    <section >
        <div >
            <div >
                <br>
                <h2 id="ultimasnovidades">ÚLTIMAS NOVIDADES</h2>
                <p>Encontra aqui as últimas novidades anunciadas.</p>
                <br>
                
            </div>
            <div >
                
                <?php
                while($reg=mysqli_fetch_array($res)){
                    $sql_consulta=sprintf("select * from eventos where id=%d;",$reg['id']);
                    $res_consulta=mysqli_query($con,$sql_consulta);
                    $_SESSION['idevento'] = $reg['id'];
                   
                    $reg_consulta=mysqli_fetch_array($res_consulta);
                    $link='eventoindividual.php?id='.$reg_consulta['id'];
                    ?>
                    <div >
                        <article >
                            <a href="<?php echo $link; ?>"> <img src="<?php echo 'admin/documentos/'.$reg['nome_doc']; ?>"  alt=""> </a>
                            <h3><a href="<?php echo $link; ?>"><?php echo $reg['nome']; ?></a></h3>
                            <span ><?php echo $reg['data']; ?></span>
                        </article>
                    </div>
                    <?php
                }
                
                ?>
                
              
            </div>
        </div>
    </section>

eventoindividual.php

<?php
session_start();
require('config.php');
if(!$con){
    header('location:erro.php');
} 

$sql=sprintf("select * from eventos where id = '".$_SESSION['idevento']."'");
$res=mysqli_query($con,$sql);

?>

<!DOCTYPE html>
<html>
<head>
    <!-- ==========================
        Meta Tags 
    =========================== -->
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <!-- ==========================
        Title 
    =========================== -->
    <title>shop</title>
        
    <!-- ==========================
        Fonts 
    =========================== -->
    <link href='https://fonts.googleapis.com/css?family=Source Sans Pro:400,200,200italic,300,300italic,400italic,600,600italic,700,700italic,900,900italic&amp;subset=latin,latin-ext' rel='stylesheet' type='text/css'>
    <link href='https://fonts.googleapis.com/css?family=Raleway:400,100,200,300,500,600,700,900,800' rel='stylesheet' type='text/css'>

    <!-- ==========================
        CSS 
    =========================== -->
    <link href="assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link href="https://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css">
    <link href="assets/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="assets/css/dragtable.css" rel="stylesheet" type="text/css">
    <link href="assets/css/owl.carousel.css" rel="stylesheet" type="text/css">
    <link href="assets/css/animate.css" rel="stylesheet" type="text/css">
    <link href="assets/css/color-switcher.css" rel="stylesheet" type="text/css">
    <link href="assets/css/custom.css" rel="stylesheet" type="text/css">
    <link href="assets/css/color/red.css" id="main-color" rel="stylesheet" type="text/css">
    
    <!-- ==========================
        JS 
    =========================== -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
    
</head>
<body>
    
    <!-- ==========================
        SCROLL TOP - START 
    =========================== -->
    <div id="scrolltop" ><i ></i></div>
    <!-- ==========================
        SCROLL TOP - END 
    =========================== -->
    
    <!-- ==========================
        COLOR SWITCHER - START 
    =========================== -->
    
    <!-- ==========================
        COLOR SWITCHER - END 
    =========================== -->
    
    <div id="page-wrapper"> <!-- PAGE - START -->
    
    <!-- ==========================
        HEADER - START 
    =========================== -->
        <div >
        <div >
            <div >
                <div >
                    <ul >
                        <li><i ></i> [email protected]</li>
                        <li><i ></i>  111111111</li>
                    </ul>
                </div>
                <div >
                    <ul >
                        <li><a href="my-account.html">Minha Conta</a></li>
                        <li><a href="checkout.html">Checkout</a></li>
                        <li><a href="wishlist.html">Lista de Desejos</a></li>
                        <li><a href="compare.html">Comparar</a></li>
                        <li><a href="signin.html">Logout</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
        
        <hr id="hrlogotipo">
    <!-- ==========================
        HEADER - END 
    =========================== --> 
    
    <!-- ==========================
        BREADCRUMB - START 
    =========================== -->
        <?php
                while($reg=mysqli_fetch_array($res)){
                    $sql_consulta=sprintf("select * from utilizadores where id=%s;",$reg['id']);
                    $res_consulta=mysqli_query($con,$sql_consulta);
                   
                    $reg_consulta=mysqli_fetch_array($res_consulta);
                    ?>
        
    <section >
        <div >
            <div >
                <div >
                    <h2><?php echo $reg['tipoevento']; ?></h2>
                    <p><?php echo $reg['artista']; ?></p>
                </div>
                <div >
                    <ol >
                        <li><a href="index.php">Inicio</a></li>
                        <li ><a href="eventoindividual.php"> <?php echo $reg['nome']; ?> </a></li>
                    </ol>
                </div>
            </div>
        </div>
    </section>
    <!-- ==========================
        BREADCRUMB - END 
    =========================== -->
    
    <!-- ==========================
        PRODUCTS - START 
    =========================== -->
        <section >
        <div >
            <article >
                <div >
                    
                    
                    <div >
                        <div >
                            <div>
                                <img src="<?php echo 'admin/documentos/'.$reg['nome_doc']; ?>" style="width: 370px;">
                                <br>
                                <br>
                            </div>
                        </div>
                    </div>
                    <div >
                        <div >
                            <h2 style="color: #337ab7;"><b><?php echo $reg['nome']; ?></b></h2>
                            
                            <hr id="nomeevento">
    
                            <span >
                                <ins><span ><b><?php echo $reg['preco']; ?></b></span></ins>
                            </span>
                            <hr>
                            <ul >
                                <li style="color: #337ab7;"><span style="color: black;"><b>ID</b></span><?php echo $reg['id']; ?></li>
                                <li style="color: #337ab7;"><span style="color: black;"><b>Classificação</b></span><?php echo $reg['classificacao']; ?></li>
                                <li style="color: #337ab7;"><span style="color: black;"><b>Sessões</b></span><?php echo $reg['data']; ?> - <?php echo $reg['hora']; ?></li>
                                <li style="color: #337ab7;"><span style="color: black;"><b>Disponibilidade</b></span><?php echo $reg['estado']; ?></li>
                                <li style="color: #337ab7;"><span style="color: black;"><b>Promotor</b></span><?php echo $reg['promotor']; ?></li>
                            </ul>
                            
                            <hr>
                            
                            <h3 style="color: black"><b><?php echo $reg['1titulodescricao']; ?></b></h3>
                            <p><?php echo $reg['primeiradescricao']; ?> </p>
                            <div >
                                <div >
                                    
                                    <div >
                                        <div >
                                            <a  id="qty-minus">-</a>
                                            <input type="text"  id="qty" value="1">
                                            <a  id="qty-plus"> </a>
                                        </div>
                                    </div>
                                    
                                    <div >
                                        <div >
                                            <form >
                                                <div >
                                                    <label>Size:</label>
                                                </div>
                                                <div >
                                                    <select >
                                                        <option>XS</option>
                                                        <option>S</option>
                                                        <option selected="selected">M</option>
                                                        <option>L</option>
                                                        <option>XL</option>
                                                        <option>XXL</option>
                                                    </select>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                    
                                    <div >
                                        <div >
                                            <form >
                                                <div >
                                                    <label>Color:</label>
                                                </div>
                                                <div >
                                                    <select >
                                                        <option selected="selected">Black</option>
                                                        <option>White</option>
                                                        <option>Red</option>
                                                        <option>Yellow</option>
                                                    </select>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                    
                                    <div >
                                        <a href="" ><i ></i>Comprar</a>
                                    </div>
                                    
                                </div>
                            </div>
                            <ul >
                                <li><a href="#"><i ></i>Add to wishlist</a></li>
                                <li><a href="#"><i ></i>Compare</a></li>
                                <li><a href="#"><i ></i>Email to friend</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            
            <div >
                <ul  role="tablist">
                    <ul  role="tablist">
                    <li role="presentation" ><a href="#description" role="tab" data-toggle="tab" aria-controls="description" aria-expanded="false">Descrição</a></li>
                    <li role="presentation" ><a href="#video" role="tab" data-toggle="tab" aria-controls="video" aria-expanded="false">Video de apresentação</a></li>
                </ul>
            
                </ul>
                <div >
                    <div role="tabpanel"  id="description">
                        <h3><b><?php echo $reg['2titulodescricao']; ?></b></h3>
                        <p><?php echo $reg['segundadescricao']; ?> </p>
                    </div>
                    <div role="tabpanel"  id="reviews">
                    
                        <div >
        
                
                <?php
                
                }
                
                ?>
                
            
                    <div >
                        <h2>Eventos Relacionados</h2>
                        <div  id="products">
                        
                    <!-- PRODUCT - START -->
                    <div >
                        <article >
                            <div >
                                <div >
                                    
                                    <?php
                
                $sqli=sprintf("SELECT * FROM eventos ORDER BY id DESC LIMIT 8");
                $resu=mysqli_query($con,$sqli);
                mysqli_set_charset($con, 'UTF8');
                    
                while($regi=mysqli_fetch_array($resu)){
                    $sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
                    $resu_consulta=mysqli_query($con,$sqli_consulta);
                   
                    $regi_consulta=mysqli_fetch_array($resu_consulta);
                    $linkk='eventoindividual.php?id='.$regi_consulta['id'];
                    ?>
                                    
                                    <div >
                                        <div ></div>
                                        <a href="single-product.html" ></a>
                                        <img src="<?php echo 'admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
                                    </div>
                                </div>
                                <div >
                                    <div >
                                        <h3><?php echo $regi['nome']; ?></h3>
                                        <span >
                                            <ins><span ><?php echo $regi['preco']; ?></span></ins>
                                        </span>
                                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut feugiat mauris eget magna egestas porta. Curabitur sagittis sagittis neque rutrum congue. Donec lobortis dui sagittis, ultrices nunc ornare, ultricies elit. Curabitur tristique felis pulvinar nibh porta. </p>
                                        <div >
                                            <a href=""><i ></i></a>
                                            <a href=""><i ></i></a>
                                            <a href=""><i ></i></a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </article>
                    </div>
                    
                </div>
            </div>
            
                        <?php
                
                }
                
                ?>

        </div>

CodePudding user response:

i think that you are taking the wrong id in your " eventoindividual.php "

Replace the following line ( L.7 )

$sql=sprintf("select * from eventos where id = '".$_SESSION['idevento']."'");

By

$sql=sprintf("select * from eventos where id = '".$_GET['id']."'");

This way you take the correct id from the URL and not the SESSION

Have a nice day !

CodePudding user response:

I guess the problem is on this line :

$sql=sprintf("select * from eventos where id = '".$_SESSION['idevento']."'");

Because you did not send the data (idevento) by SESSION , but you actually sent it using GET method

The best solution is to replace '$_SESSION['idevento']' by '$_GET['idevento']'

  • Related