需要帮助理解mySQL PHP项目

时间:2021-10-27 02:00:58

I was given an assinment to finish a project from an advanced Web Aplication student and im trying to understand all the code, but it is becoming very difficult. I'd appreciate some help with the code lines. Im already commenting the lines to help me advance. Can you comment the lines as well so I can advance in the code? Thanks for all the cooperation in advance. Its merely a challenge so no pressure for anyone, i just ask for good will kind of help. Code is in portuguese, hope it doesnt make much difference, if so, i'll clarify it.

我从一位先进的Web Aplication学生那里获得了一个项目,并试图理解所有的代码,但它变得非常困难。我很感激代码行的一些帮助。我已经评论了这些线来帮助我前进。你能否评论这些行,以便我可以在代码中前进?感谢您提前进行的所有合作。它只是一个挑战,所以对任何人都没有压力,我只是要求善意的帮助。代码是葡萄牙语,希望它没有太大的区别,如果是这样,我会澄清它。

(Code verified)

<?php

require_once("top.php"); //incluir ficheiro uma única vez

$idvoo= !empty($_GET["idvoo"]) ? $_GET["idvoo"] : null; //verifica se a variável existe ou retorna valor nulo

$query= "select a.ID, a.TotalFilas, a.LugaresPorFila_Esquerda,".
" a.LugaresPorfila_Central, a.LugaresPorFila_Direita FROM aviao as a".
" WHERE a.ID = (SELECT v.IDAviao FROM voo as v WHERE v.ID = ?)" ; //Consulta os parâmetros especificados no código da 
                                                              //respectiva tabela (aviao)
 //echo $query;
 //die;

$stmt = $mydb->prepare($query); //Prepara a consulta para a base de dados
//var_dump($mydb->error);


$stmt->bind_param("i", $idvoo); //Muda o valor do parãmetro "?" para "idvoo"

$stmt->execute();               //Executa o mysql


$result = $stmt->get_result();  //Obtem os resultados
$aviao = $result->fetch_assoc();//Cria um array associativo do resultado mysql

//var_dump($aviao);

Thank you all for your answers, they were extremely fast and I could understand a lot more of the code besause of them. Specially the statement query which i had never used before. If you still have the time I have more lines of code I need to understand, no need to comment on repeated statements such as "$stmt" "query" etc.

谢谢大家的答案,他们非常快,我可以理解更多的代码,因为他们。特别是我从未使用过的语句查询。如果你还有时间,我需要了解更多的代码行,不需要对重复的语句进行评论,例如“$ stmt”“query”等。

echo "<h1>Lugares</h1>";
echo "<table border=1>";
echo "<thead><tr><th>Fila</th>"; //Construção da Tabela Lugares

$letras = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I');//Criação de um array com as letras relativas a cada linha de lugares

$posicao = 0;
for ($i=0; $i < $aviao['LugaresPorFila_Esquerda']; $i++) { 
    echo "<th>".$letras[$posicao]."</th>";
    $posicao++;
}
for ($i=0; $i < $aviao['LugaresPorfila_Central']; $i++) { 
    echo "<th>".$letras[$posicao]."</th>";
    $posicao++;
}
for ($i=0; $i < $aviao['LugaresPorFila_Direita']; $i++) { 
    echo "<th>".$letras[$posicao]."</th>";
    $posicao++;
}

echo "</tr>";

//Consulta os parâmetros especificados no código da tabela bilhete
$query= "select b.ID, b.IDVoo, b.NumFila, b.LetraLugar,".
" b.Estado, v.ID as IDVoo FROM bilhete as b 
INNER JOIN voo as v ".
" ON b.IDVoo = v.ID".                                               
" WHERE ((b.IDVoo=?) OR (? is NULL)) AND (b.Estado='L') ORDER BY b.NumFila, b.LetraLugar" ;

$stmt = $mydb->prepare($query);



$stmt->bind_param("ii", $idvoo,$idvoo);

$stmt->execute();


$result = $stmt->get_result();

echo "<table border=1>";
        echo "<tr>";
        echo "<th>Bilhete Nº:</th>";
        echo "<th>Fila:</th>";
        echo "<th>Lugar:</th>";
        echo "</tr>";
while ($bilhete = $result->fetch_assoc()){


    //if ($bilhete["Estado"] == "L") {

        echo "<tr>";
        echo '<td class="form-control"><a class="btn btn-success btn-sm"     href="carrinho.php?id='.$bilhete["ID"].'">  Adicionar </a></td>';
        echo "<td>" .$bilhete["NumFila"]."</td>";
        echo "<td>" .$bilhete["LetraLugar"]."</td>";
        echo "</tr>";
//  }

}
echo "</table>";

3 个解决方案

#1


Here is the code commented further.

这是代码进一步评论。

//Require another File
require_once("top.php");

//Check if the Get Variable Exists else give it a value of null
$idvoo= !empty($_GET["idvoo"]) ? $_GET["idvoo"] : null;

//Create a Mysql Query for the database
$query= "select a.ID, a.TotalFilas, a.LugaresPorFila_Esquerda,".
" a.LugaresPorfila_Central, a.LugaresPorFila_Direita FROM aviao as a".
" WHERE a.ID = (SELECT v.IDAviao FROM voo as v WHERE v.ID = ?)" ;




//Prepare to query the database
$stmt = $mydb->prepare($query);

//Change the ? in the query to $idvoo declared earlier
$stmt->bind_param("i", $idvoo);

//Execute the MySQL
$stmt->execute();

//Fetch the results of the query
$result = $stmt->get_result(); 

//Create an associative array with the result
$aviao = $result->fetch_assoc();

//Set up a table with three columns (th being column header)
echo "<table border=1>";
        echo "<tr>";
          echo "<th>Bilhete Nº:</th>";
          echo "<th>Fila:</th>";
          echo "<th>Lugar:</th>";
        echo "</tr>";

    //While we have results from the database lets loop through them one by one
    while ($bilhete = $result->fetch_assoc())
    {


        //if ($bilhete["Estado"] == "L") {
        //Foreach result we have, create a new row and insert the values into the relevant columns. The first column will be a link that will pass the ID to another page for processing again using a $_GET variable
        echo "<tr>";
            echo '<td class="form-control"><a class="btn btn-success btn-sm"     href="carrinho.php?id='.$bilhete["ID"].'">  Adicionar </a></td>';
            echo "<td>" .$bilhete["NumFila"]."</td>";
            echo "<td>" .$bilhete["LetraLugar"]."</td>";
        echo "</tr>";
       // }

    }
  //Now we have looped through the results, close the table element. If there were no results we would simply see a table with heading and no rows of information
  echo "</table>";

Quite simply you are retrieving rows from the database based on the value of a $_GET variable. So try to navigate to your script like so:

很简单,您正在根据$ _GET变量的值从数据库中检索行。因此,尝试导航到您的脚本,如下所示:

http://url-to-script?idvoo=1
http://url-to-script?idvoo=2

This means that the value of $_GET['idvoo'] is 1 in the first example and 2 in the second. If you do not put the get parameter in the URL, the id it will check will be null, as can be seen by this line:

这意味着$ _GET ['idvoo']的值在第一个示例中为1,在第二个示例中为2。如果你没有把get参数放在URL中,它将检查的id将为null,如下所示:

$idvoo= !empty($_GET["idvoo"]) ? $_GET["idvoo"] : null;

#2


<?php
require_once("top.php"); // included a file

$idvoo= !empty($_GET["idvoo"]) ? $_GET["idvoo"] : null; // Assigned the value to $idvoo
$query= "select a.ID, a.TotalFilas, a.LugaresPorFila_Esquerda,".
" a.LugaresPorfila_Central, a.LugaresPorFila_Direita FROM aviao as a".
" WHERE a.ID = (SELECT v.IDAviao FROM voo as v WHERE v.ID = ?)" ; // Constructed SQL query.
//echo $query;
//die;

$stmt = $mydb->prepare($query); // Prepared SQL statement.
//var_dump($mydb->error);
$stmt->bind_param("i", $idvoo); // Bound parameter $idvoo to the question mark in query 'WHERE v.ID = ?)'
$stmt->execute(); // Executed the SQL statement so that we get results.
$result = $stmt->get_result();  // Get results from Database
$aviao = $result->fetch_assoc(); // Fetch associative array for the result set from SQL.
//var_dump($aviao);

#3


<?php

require_once("top.php"); //The statement compels the page to be run in presence of this top.php i.e. top.php must be available in the page

$idvoo= !empty($_GET["idvoo"]) ? $_GET["idvoo"] : null; //it states that if GET variable idvoo is not empty then idvoo variable will be assign a value of the same and in opposite a null value will be assigned.

$query= "select a.ID, a.TotalFilas, a.LugaresPorFila_Esquerda,".
" a.LugaresPorfila_Central, a.LugaresPorFila_Direita FROM aviao as a".
" WHERE a.ID = (SELECT v.IDAviao FROM voo as v WHERE v.ID = ?)" ;//The above query states that select some fields from table aviao with respect to selection of v.IDAviao from another table voo 
$stmt = $mydb->prepare($query);`//preparing the query..using PDO
$stmt->bind_param("i", $idvoo);//Binding the parameter to variable name..read [this][1] 

$stmt->execute();//Executing the query
`$result = $stmt->get_result(); //Getting the result



`$aviao = $result->fetch_assoc();`//storing in associative array.
//var_dump($aviao);

#1


Here is the code commented further.

这是代码进一步评论。

//Require another File
require_once("top.php");

//Check if the Get Variable Exists else give it a value of null
$idvoo= !empty($_GET["idvoo"]) ? $_GET["idvoo"] : null;

//Create a Mysql Query for the database
$query= "select a.ID, a.TotalFilas, a.LugaresPorFila_Esquerda,".
" a.LugaresPorfila_Central, a.LugaresPorFila_Direita FROM aviao as a".
" WHERE a.ID = (SELECT v.IDAviao FROM voo as v WHERE v.ID = ?)" ;




//Prepare to query the database
$stmt = $mydb->prepare($query);

//Change the ? in the query to $idvoo declared earlier
$stmt->bind_param("i", $idvoo);

//Execute the MySQL
$stmt->execute();

//Fetch the results of the query
$result = $stmt->get_result(); 

//Create an associative array with the result
$aviao = $result->fetch_assoc();

//Set up a table with three columns (th being column header)
echo "<table border=1>";
        echo "<tr>";
          echo "<th>Bilhete Nº:</th>";
          echo "<th>Fila:</th>";
          echo "<th>Lugar:</th>";
        echo "</tr>";

    //While we have results from the database lets loop through them one by one
    while ($bilhete = $result->fetch_assoc())
    {


        //if ($bilhete["Estado"] == "L") {
        //Foreach result we have, create a new row and insert the values into the relevant columns. The first column will be a link that will pass the ID to another page for processing again using a $_GET variable
        echo "<tr>";
            echo '<td class="form-control"><a class="btn btn-success btn-sm"     href="carrinho.php?id='.$bilhete["ID"].'">  Adicionar </a></td>';
            echo "<td>" .$bilhete["NumFila"]."</td>";
            echo "<td>" .$bilhete["LetraLugar"]."</td>";
        echo "</tr>";
       // }

    }
  //Now we have looped through the results, close the table element. If there were no results we would simply see a table with heading and no rows of information
  echo "</table>";

Quite simply you are retrieving rows from the database based on the value of a $_GET variable. So try to navigate to your script like so:

很简单,您正在根据$ _GET变量的值从数据库中检索行。因此,尝试导航到您的脚本,如下所示:

http://url-to-script?idvoo=1
http://url-to-script?idvoo=2

This means that the value of $_GET['idvoo'] is 1 in the first example and 2 in the second. If you do not put the get parameter in the URL, the id it will check will be null, as can be seen by this line:

这意味着$ _GET ['idvoo']的值在第一个示例中为1,在第二个示例中为2。如果你没有把get参数放在URL中,它将检查的id将为null,如下所示:

$idvoo= !empty($_GET["idvoo"]) ? $_GET["idvoo"] : null;

#2


<?php
require_once("top.php"); // included a file

$idvoo= !empty($_GET["idvoo"]) ? $_GET["idvoo"] : null; // Assigned the value to $idvoo
$query= "select a.ID, a.TotalFilas, a.LugaresPorFila_Esquerda,".
" a.LugaresPorfila_Central, a.LugaresPorFila_Direita FROM aviao as a".
" WHERE a.ID = (SELECT v.IDAviao FROM voo as v WHERE v.ID = ?)" ; // Constructed SQL query.
//echo $query;
//die;

$stmt = $mydb->prepare($query); // Prepared SQL statement.
//var_dump($mydb->error);
$stmt->bind_param("i", $idvoo); // Bound parameter $idvoo to the question mark in query 'WHERE v.ID = ?)'
$stmt->execute(); // Executed the SQL statement so that we get results.
$result = $stmt->get_result();  // Get results from Database
$aviao = $result->fetch_assoc(); // Fetch associative array for the result set from SQL.
//var_dump($aviao);

#3


<?php

require_once("top.php"); //The statement compels the page to be run in presence of this top.php i.e. top.php must be available in the page

$idvoo= !empty($_GET["idvoo"]) ? $_GET["idvoo"] : null; //it states that if GET variable idvoo is not empty then idvoo variable will be assign a value of the same and in opposite a null value will be assigned.

$query= "select a.ID, a.TotalFilas, a.LugaresPorFila_Esquerda,".
" a.LugaresPorfila_Central, a.LugaresPorFila_Direita FROM aviao as a".
" WHERE a.ID = (SELECT v.IDAviao FROM voo as v WHERE v.ID = ?)" ;//The above query states that select some fields from table aviao with respect to selection of v.IDAviao from another table voo 
$stmt = $mydb->prepare($query);`//preparing the query..using PDO
$stmt->bind_param("i", $idvoo);//Binding the parameter to variable name..read [this][1] 

$stmt->execute();//Executing the query
`$result = $stmt->get_result(); //Getting the result



`$aviao = $result->fetch_assoc();`//storing in associative array.
//var_dump($aviao);