0

I want to get the category and the order variables from a HTML code and use them in the SELECT query. I want something like this: SELECT * FROM nepal ORDER BY $category $order.

HTML code:

<!DOCTYPE html>
<html>`asdasdasdasd`
<head>

</head>
<body>

<form action="interpret.php" method="get">
  Categorie
  <select name="categorie">
    <option value="DECEDATI">Decedati</option>
    <option value="RANITI">Raniti</option>
    <option value="DISPARUTI">Disparuti</option>
    <option value="CLADIRI_DISTRUSE">Cladiri distruse</option>
    <option value="DURATA">Durata</option>
    <option value="MAGNITUDINE">Magnitudine</option>
    <option value="ADANCIME">Adancime</option>
    <option value="PAGUBE">Pagube</option>
    <option value="NUMAR_REPLICI">Numar replici</option>
  </select>
  Ordonati
  <select name="ordine">
    <option value="ASC">Crescator</option>
    <option value="DESC">Descrescator</option>
  </select>

<input type="submit"></input>
</form>
</body>
</html>

PHP code:

<?php

$conn = oci_connect('student', 'STUDENT', 'localhost/XE');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

// Prepare the statement
$stid = oci_parse($conn, 'SELECT * FROM nepal ORDER BY $_GET['categorie'] $_GET['ordine'] ');
if (!$stid) {
    $e = oci_error($conn);
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

// Perform the logic of the query
$r = oci_execute($stid);
if (!$r) {
    $e = oci_error($stid);
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

// Fetch the results of the query
print "<table border='1'>\n";
while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
    print "<tr>\n";
    foreach ($row as $item) {
        print "    <td>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>\n";
    }
    print "</tr>\n";
}
print "</table>\n";

oci_free_statement($stid);
oci_close($conn);

?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

0 Answers0