4

I'm having trouble figuring out how to implement the sql into my php code for my webpage. This is an assignment unfortunately.

If anyone could help that'd be awesome because I had someone who was going to help me today but they cancelled last minute and I'm still stuck on this.

My Majors area on the page, each major (comp sci, eng, bus) has to be clickable and when they're clicked on, it updates the index.php page and shows the students in the table that have that major. I do use majorID as a foreign key to identify majors. I'm having trouble knowing how to use php to do this.

I was thinking of using this code using a foreach loop, except when I did I get too many errors, one being undefined index and fatal uncaught pdo.

<aside>
        <!-- display a list of majors-->
        <h2>Majors</h2>
        <nav>
        <ul>
        <?php foreach ($majors as $major) : ?>
            <li>
            <a href="?majorID=<?php 
                      echo $major['majorID']; ?>">
                <?php echo $major['majorname']; ?>
            </a>
            </li>
        <?php endforeach; ?>
        </ul>
        </nav>
    </aside>

My page right now.

This is my .sql code

DROP DATABASE IF EXISTS richard_ricardo_student_db;

CREATE DATABASE richard_ricardo_student_db;
DROP USER 'richardricardo1'@'localhost';
CREATE USER 'richardricardo1'@'localhost' IDENTIFIED BY 'richardisgreat';
GRANT SELECT, INSERT, UPDATE, DELETE ON `richard\_ricardo\_student\_db`.* TO 'richardricardo1'@'localhost';

CREATE table major (
 majorID int NOT NULL AUTO_INCREMENT,
 majorname varchar(255),
 PRIMARY KEY (majorID)
);

CREATE table student (
 studentID int NOT NULL AUTO_INCREMENT,
 majorID int,
 firstname varchar(255),
 lastname varchar(255),
 gender varchar(10),
 PRIMARY KEY (studentID),
 FOREIGN KEY (majorID) REFERENCES major (majorID)
);

INSERT INTO major (majorID, majorname)
VALUES (1, "Computer Science"),(2, "Electrical Engineering"),(3, "Business");

INSERT INTO student (studentID, majorID, firstname, lastname, gender)
VALUES (1, 1, "Po", "Black", "M"),(2, 1, "Shifu", "Hoffman", "M"),(3, 1, "Tigress", "Jolie", "F"),(4, 1, "Jennifer", "Yuh", "F"),(5, 1, "Ox", "Storming", "M"),(6, 2, "Monkey", "Chan", "M"),(7, 1, "Viper", "Liu", "F"),(8, 2, "Mantis", "Rogen", "M"),(9, 3, "Crane", "Cross", "M"),(10, 3, "Oogway", "Kim", "M"),(11, 3, "Ping", "Hong", "M");

And this is my index.php code that connects to the sql database on phpmyadmin and my database.php file.

<!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="main.css?v=1">
    <title>richard ricardo kung fu school</title> 
</head>

<body>
<header>
<h1>Richard Ricardo Kung Fu School - Students</h1>
</header>

<section> 
<div id="MajorList">
<h2>Majors</h2>

<div id="MajorPadding">
<?php
include('richard_ricardo_database.php');

$stmt = $pdo->query('SELECT * FROM Major');
while ($row = $stmt->fetch())
{
    echo $row['majorname']. "<br><br>";
}
?>
</div>
</div> 
<div id="StudentList">
<h2>Student List</h2>

<table class="greyGridTable">
<thead>
<tr>
<th>Student ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th></th>
</tr>
<?php
$stmt = $pdo->query('SELECT * FROM Student');
while ($row = $stmt->fetch())
{
    echo "<tr><td>".$row['studentID']."</td><td>".strtoupper($row['firstname'])."
    </td><td>".strtoupper($row['lastname'])."</td><td>".$row['gender']."
    </td><td><a href='richard_ricardo_delete_student.php?id=".$row['studentID']."'>
    <button>Delete</button></a></td></tr>";
}
?>
</table>

<br>
<a href="richard_ricardo_add_student_form.php">Add Student</a>
<br>

<br>
<a href="richard_ricardo_add_major_form.php">List / Add Major</a>
<br>
</div>
</section>

<br>
<footer>&copy; 2015 richard ricardo kung fu school</footer>

</body>

</html>

Sorry this is such a long page, I'm also not sure how to insert smaller images. This is how it's supposed to look, if I were to click on computer science as the major for example. Oh also the database.php file uses a pdo object.

enter image description here

1 Answer 1

1

If I am understanding your question correctly you want to be able to see all students of a selected major? Your query would be something like this then with the schema you presented:

SELECT studentID, firstname, lastname, gender
FROM   student
       JOIN major ON student.majorID = major.majorID
WHERE  major.majorID = 2;

which would show all users of the electrical engineering major

EDIT:

Since this is an assignment I will try to help as much as I can without writing exact code for this part. So for the majors on the left you would associate each one with an ID and send it to the page on clicking it:

http://localhost/studentmajors.php?major=1

and in PHP you would $_GET "major" and then proceed with the previous query for the id of 1, but be careful when doing something like this as it opens up for SQL Injection.

  $major_id = $_GET['major'];

You will want to validate the input. Which I think is beyond the scope of this.

Sign up to request clarification or add additional context in comments.

5 Comments

That does get the table to show up with only those students with that major. Should I be doing this in an if else if statement so if I click on computer science link it'll show the students with majorID = 1?
Thank you very for your help sqlsquirrel. I believe I am understanding better, I am still a bit stuck on getting the major in php. I've assigned each link an id though I'm stuck on getting the link to update with the info.
@Saiba Irevised the edit, but also check here: teamtreehouse.com/community/…
Oh, okay, I definitely got it now. Thank you again so much for your help! :)
@Saiba You're welcome. If you feel this was an acceptable solution if you could accept the answer. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.