Reviewing sample PHP code that interacts with a MySQL Database
In our previous articles, we've created a simple website that allows for users to submit comments about an article. In this article we are going to walk through what happens when someone submits a comment in our test website.
Step 1 - Look at the data currently in the database
Before we post any comments to our database, we'll want to look at our comments table to see what is in it. To do this:
- Log into your cPanel and click the phpMyAdmin icon
- Click your database in the left sidebar, and then click on your table. If you're following our example, we'll first click on "_mysite" and then "comments".
- On the right side of the page, you'll see all of the comments submitted. If you refer to our screenshot below, you'll see we currently only have one comment, which is our test comment from a previous article:
Step 2 - Submit a comment on your website
At this point, we will replicate what a user will do, and that is leave a comment. To do this:
- Visit your first page, http://phpandmysql.inmotiontesting.com/page1.php?id=1. It is very important that "id=1" is in the URL, otherwise our php code will not know which article the comment belongs to.
- Input a comment and click submit (refer to screenshot below):
At this point, we'll want to run the test again, but this time on page2. Click the link on the page to go to page2.php and insert another comment.
Step 3 - Confirm that your data has been written to the database
Now that we have comments in our database, we'll show you in our next article how you can access the database and display those comments on each page.
‹Section 5: Using PHP to INSERT data into a database
›Section 7: How to use PHP to connect to and retrieve data from MySQL
We value your feedback!
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
new! - Enter your name and email address above and we will post your feedback in the comments on this page!
Using PHP to create dynamic pages
2014-08-21 2:06 am
i tried and follow full example but the problem which i face is that my include statmnt not working all e.g <? include("manage_comments.php"); ?>
as well the code for formcode and so on where include function used ..
is it any alternative for all include stamnts which all used in this example.
2014-08-21 7:47 am
Include statements only serve to add the php that is in those files to the current file. You can certainly forgo any include statement and code the php there in the file. Includes are best used to separate code or to reuse the code in other files.
What type of error are you getting when you run the program regarding the include file? Some other formats for using include are:
<? include('manage_comments.php'); ?>
<? include 'manage_comments.php' ; ?>
In the examples above the file should exist in the same folder as the file that has the include statement.
2014-10-24 12:01 pm
I did exactly as stated in this tutorial. I submitted a comment from website and i gave a message as you mentioned "Thank you very much..."
But it didnt get updated in table. Any clue what could be the problem?Thanks
2014-10-24 12:13 pm
You will want to start looking through the code. I would start where the code attempts to update the database. Does it give an error message when the update attempts to run? If so, what is the message? You may need to alter the code a bit to display this information. For example, change the code:
To something like:
$return = mysql_query($query);
And then have it print out the variable to the screen:
This is a debug method so you will not want to leave it in the code after you check.