InMotion Hosting Support Center

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:

  1. Log into your cPanel and click the phpMyAdmin icon
  2. 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".
  3. 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:
    comments-table-in-phpmyadmin-with-one-test-record

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:

  1. 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.
  2. Input a comment and click submit (refer to screenshot below):
    inserting-our-first-comment-on-a-page

The comment has been Posted to the server and we now see a confirmation, "Thank you for your Comment!":
after-we-have-submitted-our-first-comment

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've submitted 2 test comments, we should be able to see them in the database. Follow the same steps from "Step 1" above to view the new comments in the database:
looking-at-phpmyadmin-after-2-test-comments

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.

Continued Education in Course 205: Using PHP to create dynamic pages
You are viewing Section 6: Reviewing sample PHP code that interacts with a MySQL Database
Section 5: Using PHP to INSERT data into a database
Section 7: How to use PHP to connect to and retrieve data from MySQL

Support Center Login

Social Media Login

   
Social Login Joomla

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question
n/a Points
2014-06-25 5:21 am

useful

n/a Points
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.

Staff
29,167 Points
2014-08-21 7:47 am
Hello Yasir,

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.

Kindest Regards,
Scott M
n/a Points
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

 

Staff
29,167 Points
2014-10-24 12:13 pm
Hello John,

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:
mysql_query($query);


To something like:
$return = mysql_query($query);


And then have it print out the variable to the screen:
echo $return;


This is a debug method so you will not want to leave it in the code after you check.

Kindest Regards,
Scott M
n/a Points
2015-08-07 7:05 am

Hello,

This will be an answer instead of a question. I had the same problem when I submited the comments my database didn't get updated.

I deleted the ";" just after the bracket at the end, and now viola! it works =) I hope this will help.

INSERT INTO `inmoti6_mysite`.`comments` (`id`, `name`, `email`, `website`,
        `comment`, `timestamp`, `articleid`) VALUES (NULL, '$users_name',
        '$users_email', '$users_website', '$users_comment',
        CURRENT_TIMESTAMP, '$articleid');";
n/a Points
2015-01-29 10:43 am

I followed the tutorial from the beginning . I built the 4 pages in dreamweaver under one of my domains , was that a mistake ? I have not uploaded the 4 pgs yet , what should I do ?  I built the DB as instructed and copied the following as instructed , but have done anything with this yet , the instruction were to save it for use later  INSERT INTO `flamer5_TendersDB`.`tenders` (`id`, `email`, `website`, `opinion`, `timestamp`, `articleid`) VALUES ('john smith ', 'johns@domain.com', 'johnsmith.com', 'this is a test comment. ', CURRENT_TIMESTAMP, '');

Staff
17,726 Points
2015-01-29 1:52 pm
Hello Bill,

Thank you for contacting us. This guide is Section 6 of a tutorial series.

I recommend following the full series on Using PHP to create dynamic pages, since this is one of the last steps.

This will help you setup the initial pages, and database correctly.

If you have any further questions, feel free to post them below.

Thank you,
John-Paul
n/a Points
2015-04-17 9:36 am

I have no idea what i'm doing wrong I followed every step. and stll getting the page no longer exist. 

Staff
29,167 Points
2015-04-17 10:00 am
Hello,

I would like to take a look at your specific issue. Do you have a link you can provide that will display the error?

Kindest Regards,
Scott M
n/a Points
2015-04-17 10:11 am

when i put in the url in my browser that is the error message that I receive. And I did every step down to a T. 

http://phpandmysql.inmotiontesting.com/page1.php?id=1

Staff
29,167 Points
2015-04-17 10:37 am
Hello Franklin,

If that is the exact link you are using, you will need to replace 'phpandmysql.inmotiontesting.com' with your own domain name so it can find the right file.

Kindest Regards,
Scott M
2015-04-24 2:05 pm
how do i add the id=1 in my url. i am confused. i tried saving my file with page1.php?id=1, but it wont save. how do i then add the id in my url
Staff
26,238 Points
2015-04-24 5:07 pm
Hello Mosaik,

We're not quite sure what you mean by your question. You would not need to save that portion of the URL, unless you're adding it to a re-direct in the .htaccess file.

Regards,
Arnel c.
n/a Points
2015-07-12 5:33 pm

I'm with Mosaik. How do you assign the page an ID so the form knows what to get?

n/a Points
2015-05-31 9:36 pm

I did exactly as stated in this tutorial. but after I submitted a comment from website,it did not show the message"Thank you for your Comment!" neither the comment been saved into database,what is wrong with my code?

page1.php:

<?PHP include("manage_comments.php"); ?><h1>This is page1.php</h1><div><a href='page2.php?id=2'>Click here</a> to go to page2.php</div><div style='margin:20px; width:100px; height:100px; background:blue;'></div><?PHP include("formcode.php"); ?>

 

page2.php:

<?PHP include("manage_comments.php"); ?><h1>This is page2.php</h1><div><a href='page1.php?id=1'>Click here</a> to go to page1.php</div><div style='margin:20px; width:100px; height:100px; background:blue;'></div><?PHP include("formcode.php"); ?>

formcode.php:

<?PHP include("manage_comments.php"); ?><h1>This is page1.php</h1><div><a href='page2.php?id=1'>Click here</a> to go to page2.php</div><div style='margin:20px; width:100px; height:100px; background:blue;'></div><?PHP include("formcode.php"); ?>

manage_comments.php:

<?PHPif( $_POST ){  $con = mysql_connect("localhost","root"," ");  if (!$con)  {    die('Could not connect: ' . mysql_error());  }  mysql_select_db("inmoti6_mysite", $con);  $users_name = $_POST['name'];  $users_email = $_POST['email'];  $users_website = $_POST['website'];  $users_comment = $_POST['comment'];  $users_name = mysql_real_escape_string($users_name);  $users_email = mysql_real_escape_string($users_email);  $users_website = mysql_real_escape_string($users_website);  $users_comment = mysql_real_escape_string($users_comment);  $articleid = $_GET['id'];if( ! is_numeric($articleid) ) {die('invalid article id'); }  $query = "  INSERT INTO `inmoti6_mysite`.`comments` (`id`, `name`, `email`, `website`,        `comment`, `timestamp`, `articleid`) VALUES (NULL, '$users_name',        '$users_email', '$users_website', '$users_comment',        CURRENT_TIMESTAMP, '$articleid');";    $return = mysql_query($query);  echo  $return;  echo "<h2>Thank you for your Comment!</h2>";  mysql_close($con);}?>

 

 

Staff
29,167 Points
2015-06-01 2:31 pm
Hello Hui,

The "echo $return;" portion of your code should return either a code or a message on the screen. Can you tell us what that message is?

Also, are you able to run the query portion by itself in the database? If so, then that would not be the issue. You would need to step through the code step by step verifying each portion to find the issue.


Kindest Regards,
Scott M
n/a Points
2015-06-02 7:30 pm

Hi Scott, thanks a lot for your reply! I actually sorted it out partly myself yesterday, I got the basic code work at least and after click submit it shows the record in database now and also prints. but that's only when I take out that articleid part, now when I add back the code with articleid part, it shows "invalid article id" on the page after submitted, so I'm pretty sure the problem relates to articleid thing, I have aritcleid together with id, name ,email, comment and timestamp in my database table, except my setting for id is auto and timestamp as current-timestamp, I didn't do any setting for the article id except the length, is that the problem from you think? Or it's from the code itself? the code is here:

test4.php:

<html><head></head><?php$con = mysql_connect("localhost","root","");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("test1", $con);$users_name = mysql_real_escape_string($_POST['name']);$users_email = mysql_real_escape_string($_POST['email']);$users_comment = mysql_real_escape_string($_POST['comment']);$articleid = $_GET['id'];  if(! is_numeric($articleid) ){  die('invalid article id');}`$sql="INSERT INTO comment (name, email, comment)VALUES('$users_name','$users_email','$users_comment','$articleid')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "1 record added";mysql_close($con);?><body></body>

 

form code:

<html>    <body>    <form action="test4.php" method="post">  NAME: <input type='text' name='name' id='name' /><br />  Email: <input type='text' name='email' id='email' /><br />  Comment:<br />  <textarea name='comment' id='comment'></textarea><br />  <input type='hidden' name='articleid' id='articleid' value='<? echo $_GET["id"]; ?>' />  <input type='submit' value='Submit' />   </form>    </body>    </html>

Staff
5,224 Points
2015-06-02 7:59 pm
Hello Hui,

If you put just echo $_GET["id"]; on your forum page wrapped in php tags, does it display the article id?

Best Regards,
TJ Edens
n/a Points
2015-07-27 9:26 am

After everything it display "invalid article id" here are my codes

HTML

<div id="contact"> <!--Comments begin-->

<form method='post'>

 NAME: <input type='text' name='name' id='name' /><br />

 

 Email: <input type='text' name='email' id='email' /><br />

 

 Website: <input type='text' name='website' id='website' /><br />

 

 Comment:<br />

 <textarea name='comment' id='comment'></textarea><br />

 

 <input type='hidden' name='articleid' id='articleid' value='<? echo $_GET["id"]; ?>' />

 

 <input type='submit' value='Submit' />  

</form>

 

 

</div>

 

PHP

<?

if( $_POST )

{

  $con = mysql_connect("localhost","root", "");

 

  if (!$con)

  {

    die('Could not connect: ' . mysql_error());

  }

 

  mysql_select_db("newspapers", $con);

 

  $users_name = $_POST['name'];

  $users_email = $_POST['email'];

  $users_website = $_POST['website'];

  $users_comment = $_POST['comments'];

 

  $users_name = mysql_real_escape_string($users_name);

  $users_email = mysql_real_escape_string($users_email);

  $users_website = mysql_real_escape_string($users_website);

  $users_comment = mysql_real_escape_string($users_comment);

 

  $articleid = $_GET['id'];

  if( ! is_numeric($articleid) )

    die('invalid article id');

 

  $query = "

  INSERT INTO `newspapers`.`comment` (`id`, `name`, `email`, `website`,

        `comments`, `date`, `articleid`) VALUES (NULL, '$users_name',

        '$users_email', '$users_website', '$users_comment',

        CURRENT_TIMESTAMP, '$articleid');";

 

  mysql_query($query);

 

  echo "<h2>Thank you for your Comment!</h2>";

 

  mysql_close($con);

}

 

?>

Staff
29,167 Points
2015-07-27 3:07 pm
Hello,
The portion of the code that is giving you that error is:

$articleid = $_GET['id'];
if( ! is_numeric($articleid) ) die('invalid article id');

It means that the article ID is not a number. You will want to check your code to see what the article ID is being set to and make any code adjustments from there.

Kindest Regards,
Scott M
n/a Points
2015-08-18 11:33 am

How come this works (based on your code):

<?php

if( $_POST )

{

  $con = mysql_connect("localhost","root@localhost","");

  if (!$con)  {

    die('Could not connect: ' . mysql_error());

  }

  mysql_select_db("test_db", $con);

 

  $users_name = $_POST['name'];

  $users_comment = $_POST['comment'];

 

  $users_name = mysql_real_escape_string($users_name);

  $users_comment = mysql_real_escape_string($users_comment);

 

  $query = "

  INSERT INTO `test_db`.`test_table`

(`id`, `name`, `comment`, `timestamp`) 

VALUES 

(NULL, '$users_name', '$users_comment', CURRENT_TIMESTAMP);

";

 

  mysql_query($query);

 

  echo "<h2>Thank you for your Comment!</h2>";

 

  mysql_close($con);

}

?>

 

and this don't?

 

<?php

if( $_POST )

{

  $con = mysql_connect("localhost","root@localhost","");

  if (!$con)  {

    die('Could not connect: ' . mysql_error());

  }

  mysql_select_db("db", $con); //db is not my database name. just a sample for this post

 

  $varname = $_POST['name'];

  $varemail= $_POST['email'];

  $varmobile = $_POST['mobile'];

  $varmsg = $_POST['message'];

  

  $varname = mysql_real_escape_string($varname);

  $varmsg= mysql_real_escape_string($varmsg);

 

  $query = "

  

INSERT INTO `db`.`inquiry` (`id`, `name`, `email`, `mobile`, `message`, `timestamp`) 

VALUES (NULL,

'$varname', 

'$varemail',

'$varmobile', 

'$varmsg', 

CURRENT_TIMESTAMP);

";

 

  mysql_query($query);

 

  echo "<h2>Thank you for your Inquiry!</h2>";

 

  mysql_close($con);

}

?>

 

please help.

there's really no difference between the codes at all except of course, on my added variables and a different database table.

It pisses me off not knowing the error.

Thanks. I appreciate the help

PS: I've tried some of the solutions in the earlier comments.

Staff
5,224 Points
2015-08-18 2:33 pm
Hello,

You do not need to add id or Null to your query as usually id is set to auto increment. Also try to error trap the code by doing var_dump($query); and make sure that it spits out a MySQL query that would be valid.

Best Regards,
TJ Edens
n/a Points
2015-08-19 1:55 am

well the var_dump($query); worked. here's the output

string(204) " INSERT INTO `db`.`inquiry` (`name`, `email`, `mobile`, `message`, `timestamp`) VALUES ('Sam Paul', 'sampaul@mail.com', '2', 'Sampaul Message', CURRENT_TIMESTAMP); "

 

 

And I've removed the id and the null value.

looks like there's no error at all. 

still no difference - no data inserted in the table :\

 

I really appreciate the help 

Staff
26,238 Points
2015-08-19 10:58 am
Hello PHP Newbie,

It doesn't look like you've done what TJ suggested in testing the mySQL query valid output. Additionally, I would suggest that you try a different forum if you're looking for help with specific coding. Providing specific coding support is beyond our normal scope of support. Try a PHP coding forum like this one: phpFreaks.com.

Regards,
Arnel C.
n/a Points
2015-08-24 2:29 pm

Good afternoon evryone,

First of all, thank you very much for your tuto. I've never coded in HTML before so it is very useful ! 

Unfortunately, I can't make it work, I dont know why ! If you guys can help ... Please find attached my code. Thank you !

Sincerely,

 

Lauren

 

in my HTML file :

<div id="Signup"> <!--Comments begin-->

<form method='post'>

  First Name: <input type='text' name='First Name' id='First Name' />

  Last Name: <input type='text' name='Last Name' id='Last Name' /><br />

  <br>

  Email: <input type='text' name='Email' id='Email' /><br />

 <br>

  <input type='submit' value='Submit' />  

</form>

</div>

 

in my php file :

<?

if( $_POST )

{

  $con = mysql_connect("localhost","EmailsMeetova","***");

 

  if (!$con)

  {

    die('Could not connect: ' . mysql_error());

  }

 

  mysql_select_db("ContactsList", $con);

 

  $users_firstname = $_POST['First Name'];

  $users_lastname = $_POST['Last Name'];

  $users_email = $_POST['email'];

 

  $users_firstname  = mysql_real_escape_string($users_firstname);

  $users_lastname  = mysql_real_escape_string($users_lastname);

  $users_email = mysql_real_escape_string($users_email);

 

 

  $query = "

 INSERT INTO `EmailsMeetova`.`ContactsList` (`First Name`, `Last Name`, `Email`, `Date`) VALUES ('Test', 'test', 'test@a.com', CURRENT_TIMESTAMP);

 

  mysql_query($query);

 

  echo "<h2>Thank you!</h2>";

 

  mysql_close($con);

}

?>

Staff
29,167 Points
2015-08-24 4:48 pm
Hello,

What specifically are you having an issue with? What is the behavior? Any error messages?

Kindest Regards,
Scott M
n/a Points
2015-08-25 9:07 am

Hi Scott, just saw your answer. I changed one line of the code. So, now, when i click Submit, I am redirected to a page with my php code.

This is my html code ( I just added action="Signup.php"), the php code is still the same. 

Thank you very much ! 

<form action="Signup.php"  method="post" accept-charset="UTF-8">

  First Name: <input type="text" name="First Name" id="First Name" />

  Last Name: <input type="text" name="Last Name" id="Last Name" /><br />

  <br>

  Email: <input type="text" name="Email" id="Email" /><br />

 <br>

  <input type="submit" value="Submit" />  

</form>

</div>

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

28 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!