0

I am creating a daycare student registration system. I have a form which submits data into my daycare table. I have a column named Grade and I would like to retrieve all students based on the selected grade. Can anyone assist me in constructing the proper logic to do this? Thanks

Here is my HTML code

<form method="POST" action="display.php">
View Student Records
<select name="grades">
<option value="all">All</option>
<option value="3">3rd Grade</option>
<option value="4">4th Grade</option>
<option value="5">5th Grade</option>
<option value="6">6th Grade</option>
</select>
<input type="submit" value="View Students" name="view">
</form>  

PHP code this is my attempted logic if the user selects 3rd graders. my grade field is also a text format rather than number

<?php
session_start();
require("dbconnect.php");
if(empty($_SESSION['user_name']))
{
echo "Please login: <a href='./index.php>Login</a>";
exit();
}
?>

<?php 
$link=Connect();
if (isset($_POST['view'])){

$grade = ($_POST['grades']);
$select = "SELECT * daycare WHERE Grade = $grade";
$result = mysql_connect($select, $link) or die("Error: "mysql_error()); 
}


echo "<table>";

while($row = mysql_fetch_array($select){   
echo "<tr><td>" . $row['fname'] . "</td><td>" . $row['lname'] . "</td></tr>" . "</td><td>" . $row['grade'] . "</td></tr>";  
}

echo "</table>"; 
}


mysql_close(); 

?>
D0uble0
  • 175
  • 1
  • 2
  • 12
  • Your code is hack able. Also : http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Kisaragi Dec 17 '15 at 16:28
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Dec 17 '15 at 16:30
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 17 '15 at 16:30
  • You have syntax errors, such as lines without closing semi-colons. You need to add some error checking to your PHP and to your queries. There is a lot going on here that needs to be fixed. There are links in the comments above that will teach you how to do this well and properly. – Jay Blanchard Dec 17 '15 at 16:31
  • you realize there's something missing here `$query3 = "SELECT * FROM daycare WHERE Grade = "3"" ` – Funk Forty Niner Dec 17 '15 at 16:32
  • btw, only ` – Funk Forty Niner Dec 17 '15 at 16:33
  • @JayBlanchard thanks for the advice – D0uble0 Dec 17 '15 at 16:39
  • Change the values in the select options to match what's in the database table. Then you can use `$_POST['grades']` in the `WHERE` clause to find the selected grade. – Barmar Dec 17 '15 at 16:53

1 Answers1

0

hellow maybe you can try this

file to connect to database:

<?php
function Connect()
{
    if (!($link=mysql_connect("localhost","root","")))
    {
        echo "Error to connect to DataBase.";
        exit();
    }
    if (!mysql_select_db("DataBase_Name",$link))
    {
        echo "Error to select DataBase.";
        exit();
    }
    return $link;
}

?>

file to select grades and show

<form method="post" action="display.php">
View Student Records
<select name="grades">
<option name="all" value="all">All</option>
<option name="third" value="third">3rd Grade</option>
<option name="fourth" value="fourth">4th Grade</option>
<option name="fifth" value="fifth">5th Grade</option>
<option name="sixth" value="sixth">6th Grade</option>
</select>
<input type="submit" value="View" name="view">
</form>

file to select and return the table grades:

<?php
session_start();
require("dbconnect.php");
if(empty($_SESSION['user_name']))
     {
           echo "session not started please login: <a href='./login.php'>Login</a>";
             exit();
         }

?>



<?php
$link=Connect(); 

if (isset($POST['view'])){

$query3 = "SELECT * FROM daycare WHERE Grade = "3"" ;
$result3=mysql_query($query3,$link) or die("Error: ".mysql_error());


}


echo "<table>";

while($row = mysql_fetch_array($result3){   
echo "<tr><td>" . $row['fname'] . "</td><td>" . $row['lname'] . "</td></tr>" . "</td><td>" . $row['grade'] . "</td></tr>";  
}

echo "</table>"; 

mysql_close($link); 

?>

and you should verify if exist values (all - third - fourth....) with a if and show the table grade from your selection.

BlackHack123
  • 349
  • 1
  • 10