需要帮助理解 mySQL PHP 项目
Need help understanding mySQL PHP project
我被要求完成一个高级 Web 应用程序学生的项目,我试图理解所有代码,但它变得非常困难。我很感激对代码行的一些帮助。我已经在评论台词以帮助我前进。您也可以评论这些行,以便我可以推进代码吗?感谢您的提前合作。这只是一个挑战,所以对任何人都没有压力,我只是寻求善意的帮助。代码是葡萄牙语,希望不会有太大区别,如果是这样,我会澄清的。
(代码已验证)
<?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);
谢谢大家的回答,他们非常快,因为他们我可以理解更多的代码。特别是我以前从未使用过的语句查询。如果你还有时间我还有更多的代码需要理解,就不用评论重复的语句,比如“$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>";
<?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);
这里是进一步注释的代码。
//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>";
很简单,您是根据 $_GET
变量的值从数据库中检索行。因此,请尝试像这样导航到您的脚本:
http://url-to-script?idvoo=1
http://url-to-script?idvoo=2
这意味着 $_GET['idvoo']
的值在第一个示例中为 1,在第二个示例中为 2。如果你没有把 get 参数放在 URL 中,它将检查的 id
将为 null,如这一行所示:
$idvoo= !empty($_GET["idvoo"]) ? $_GET["idvoo"] : null;
<?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);
我被要求完成一个高级 Web 应用程序学生的项目,我试图理解所有代码,但它变得非常困难。我很感激对代码行的一些帮助。我已经在评论台词以帮助我前进。您也可以评论这些行,以便我可以推进代码吗?感谢您的提前合作。这只是一个挑战,所以对任何人都没有压力,我只是寻求善意的帮助。代码是葡萄牙语,希望不会有太大区别,如果是这样,我会澄清的。
(代码已验证)
<?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);
谢谢大家的回答,他们非常快,因为他们我可以理解更多的代码。特别是我以前从未使用过的语句查询。如果你还有时间我还有更多的代码需要理解,就不用评论重复的语句,比如“$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>";
<?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);
这里是进一步注释的代码。
//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>";
很简单,您是根据 $_GET
变量的值从数据库中检索行。因此,请尝试像这样导航到您的脚本:
http://url-to-script?idvoo=1
http://url-to-script?idvoo=2
这意味着 $_GET['idvoo']
的值在第一个示例中为 1,在第二个示例中为 2。如果你没有把 get 参数放在 URL 中,它将检查的 id
将为 null,如这一行所示:
$idvoo= !empty($_GET["idvoo"]) ? $_GET["idvoo"] : null;
<?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);