My first PHP and MySQL Project at ElitePath
Are you having trouble visualizing, how certain concepts are applied in PHP and MySQL? Mock projects and practice work are a great way to reinforce the key concepts, you’ve learned in PHP and MySQL.
I’ve been learning PHP and a little of MySQL at ElitePath for 3weeks. I had a fair understanding of the course but some concepts still seemed a bit abstract to me.
After struggling with my project for a week, I now have a better understanding of the course and I believe my approach to handling tasks has improved to some extent.
Projects are not easy, especially the group projects, but you’ve just got to toughen up and face them down to the very end. No matter how your end, turns out to be.
Our class was shared into two project teams and I was made the group leader of my group. This scared the hell out of me because I was responsible for the overall performance of the project and I also had to make sure that my team mates, were keeping up with the project.
Our group project was on creating a chat forum. We were to replicate and redesign the work on this site:
The site above was meant to serve as our project guide. There are step by step guide lines on: creating the tables, for the MySQL database and the PHP web pages too. But it’s still quite tricky and requires lots of thinking, just like our tutor pointed out to us.
Our tutor also warned us about using outdated PHP code. We were to change some of the codes used from the project guide because the information on the site was posted in 2010. This is 2018 and there are bound to be changes over the years.
After thinking over the best course of action, I decided the best approach was to start with the functionality and leave my team members to handle the designs. We only had a week for the project, so over the weekend I created the database, tables and the PHP web pages.
I made sure the PHP webpages were functional by creating a connection to the MySQL database. I changed the outdated codes and was able to insert and fetch from the database with the PHP web pages.
But that was the easy part. I still had to apply the concept of sessions to the PHP webpages. I needed sessions to be able to sign in and sign out of the PHP web pages. I also needed sessions to be able to restrict users from viewing pages when they are not signed in.
Sessions were not part of the project guide and I had to figure that out on my own. I googled and visited:
Sessions have to be started on all your pages using: session_start(); To restrict users from viewing pages when they are not signed in, you need to have your if statement with a condition to either redirect the users to a different page or show some message, telling them to sign in.
For example, the code below displays a message if the user is has not signed in.
if(!isset($_SESSION[‘signed_in’]) || $_SESSION[“signed_in”] !== true)
{
echo ‘Sorry, you have to be <a href=”signin.php”>signed in</a> to create a category.’;
exit;
}
Don’t worry about the lengthy if condition. It’s actually not so complicated once you get used to the symbols used inside it.
!isset : that part of the code is checking if the session is not set. That is, if the user is not signed in.
$_SESSION[ ] : that’s a predefined variable in PHP. It holds the value of your session.
With those explained, I’m sure you can now get the gist of what’s going on in the session code above.
But that wasn’t the end of my problems. The chat forum is meant to have the following functions:
1. Display a list of categories for discussion on the index page
2. Users should be able to click on each category to view the topics
3. Users should be able to click the topics to view posts and replies from users
4. Users should be able to submit replies
5. An option to create topics on the Create Topic page
6. An option to create categories on the Create Category page
7. Create Category page should be available only to users that signed in
8. Create Topic page should be available only to users that signed in
By the end of the weekend, I was able to accomplish the functions 1, 2, 5, 6, 7 and 8 according to the tasks l listed above.
After the weekend, I got back to school and met with my team mates and I spent a lot of time, putting them through on our tasks and explaining what I had accomplished. This helped to further reinforce my learning.
I had a good start but things didn’t go the way I imagined. My Create Topic page stopped working and I got this fancy error message:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near “ at line 10)
I struggled with this error for days and my tutor also made some suggestions too but nothing was helping. The strange thing was, the Create Topic web page later worked in class and then stopped working the following day.
I decided to try testing my project on my team mates systems and it worked perfectly well. I was so frustrated at this point. So I took a day off and decided to just clear my head and focus on other stuff.
I came back the following day and searched online again and happened to come across some information about using proper quotes for insert statements
I’ve closed the string values properly now as shown below:
$sql = “INSERT INTO
posts(post_content, post_date, post_topic, post_by)
VALUES
(‘ “ . mysqli_real_escape_string($connection, $_POST[‘post_content’]) . “ ‘,
NOW(),
‘ “ . $topicid . “ ‘,
‘ “ . $_SESSION[‘user_id’] . “ ‘
)”;
This particular section was the cause of all my problems. I didn’t enclose some of the values properly. You have to use as single quote and double quote to enclose the values.
Like this : ‘ “ . VALUE . “ ‘
But now I’ve got this error to deal with:
#1452 — Cannot add or update a child row: a foreign key constraint fails (`chatdb`.`#sql-dc8_20ce`, CONSTRAINT `#sql-dc8_20ce_ibfk_1` FOREIGN KEY (`topic_cat`) REFERENCES `categories` (`cat_id`) ON DELETE CASCADE ON UPDATE CASCADE)
This one is a clearer problem. It’s obvious that it’s my foreign keys that are the problem. But I’m not really good at MySQL queries yet, so I’ll have to study on that.
I took a break on debugging the error and decided to focus on designing the PHP web pages. So far I’ve been able to redesign the web pages on the project guide to some extent
This a picture of the project guide without any editing:

This is what the chat forum project looks like now:

Although this isn’t the final design and I’ll have to meet up with my group members, to see what they’ve come up with. We’ll probably make more changes.
But so far I’m happy with my progress, even though my database is currently messed up. I also haven’t been able to get the user reply part to work yet.
I’m hoping I’ll be able to fix the issues by next week. My experience was quite rewarding and I wouldn’t want to erase any of it. (Including the frustrating parts of my experience.)
I hope my experience becomes useful to other newbies working on PHP projects. Don’t give up! You’ll get to that Eureka moment eventually.
Thank you so much for reading my post. I wish all newbies struggling with Projects out there, the best of luck!
