First thing first. You will need a little bit knowledge of web programming. Simple HTML will work. Now here is a step by step procedure on how to connect php to mysql database using local server. Lets create a simple comment box. And I will teach you how to do this on both Mac or windows/Linux.
Here is a list of Applications you need before doing some coding.
Now lets get into some simple coding. Since we are going to make a simple comment box, lets create two files "comment.php"and "comment_post.php" inside htdocs folder.
Note : Your default server directory is htdocs. Server will only host the files inside the htdocs directory. But you can change it in your xammp/mamp settings.
But before writing any codes, lets create a database.
filename : 'comment_post.php'
Now goto your browser and hit 'localhost/comments.php'
Your Comment box using html and php should be working perfectly fine.
Here is a list of Applications you need before doing some coding.
- A text editor ( I would recommend Sublime text)
- A local server software (MAMP for mac , Xampp for windows/linux)
If you are on mac follow the screenshot to start up the servers.
Bravo ! Your computer is now a server (local though).
If you are on windows/Linux follow the similar procedure to start your servers.
If you are on windows/Linux follow the similar procedure to start your servers.
Now lets get into some simple coding. Since we are going to make a simple comment box, lets create two files "comment.php"and "comment_post.php" inside htdocs folder.
Note : Your default server directory is htdocs. Server will only host the files inside the htdocs directory. But you can change it in your xammp/mamp settings.
But before writing any codes, lets create a database.
- After starting server, open your browser goto 'http://localhost/phpMyAdmin/'
- Create a new database name 'comments'
- Create a table 'comments' with fileds '2'
- Fill up the database table as below and save (leave other fields empty).
- Field Type Length/values
- Name varchar 100
- Comment varchar 100
Your database table looks something like this
Now Lets get into some coding !!
filename : 'comments.php'
<!DOCTYPE html> <head> </head><body> <h1> HTML comment box using PHP and MYSQL</h1> <form action="comment_post.php" name="comments" method="post" enctype="multipart/form-data"> name: <br /> <input type="text" name="name"> <br /> comment: <br /> <textarea name="comment" rows="2" cols="50"></textarea> <br /> <input type="submit" value="submit !"> </form> <br /></body></html> <?php $user = 'root'; $password = 'root'; $db = 'comments'; $host = 'localhost'; $conn= mysqli_connect( $host, $user, $password, $db); $sql = "SELECT * FROM comments" ; $result=mysqli_query($conn,$sql); if (!$conn) { echo "Unable to connect to DB: " . mysqli_error(); exit; } else { while($row = mysqli_fetch_assoc($result)) { $name=$row['name']; $comment=$row['comment']; echo'<font size="4" color="purple"> <b> '.$name. ' </b>: '.$comment.' </font> <hr></div> '; } }
filename : 'comment_post.php'
<!DOCTYPE html>
<head><meta http-equiv="refresh" content="1; url=comments.php" />
<title></title> </head>
<body>
</body>
</html>
<?php
$name=$_POST['name'];
$comment=$_POST['comment'];
$hostname="localhost";
$username="root";
$password="root";
$db="comments";
$conn=mysqli_connect($hostname,$username,$password,$db);
$sql="INSERT INTO comments(name,comment) VALUES ('$name','$comment') ";
//default password for mamp is "root" and for xampp is empty
$query=mysqli_query($conn,$sql);
if(!query)
{
echo "could not insert into database <br>";
}
else
{
echo " Sucessfully inserted inside database";
}
?>
Congratulations , You did it !!
Now goto your browser and hit 'localhost/comments.php'
Your Comment box using html and php should be working perfectly fine.
If you get any error on connecting to the database, please leave a comment
ReplyDeleteYes bro when I do it in android local host it shows error and does not work.
DeleteHow to fit it
Very nice article!! IntelliMindz is the best IT Training in Bangalore with placement, offering 200 and more software courses with 100% Placement
DeleteAssistance.
Java Training In Bangalore
LoadRunner Training In Bangalore
TestComplete Training In Bangalore
SDET Training In Bangalore
Very good explanation, Thanks for sharing.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteHi!
ReplyDeleteThis is a very interesting tutorial. But how can se do to display comment into boxes (bubbles for example).
Thanks
hi....i have created this comment box...but i want to display comments on page....how can i do it??
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis data is extremely helpful. Continue sharing these sort of valuable and enlightening article refreshes.
ReplyDeleteMBA Talks | Article Submission sites
It is a wonderful data you offered to us I really enjoy by reading your article.
ReplyDeletephp course
php courses in chennai
Hi, Excellent Content, your blog is very useful and also interesting to read. Keep sharing this type of information.
ReplyDeletespring hibernate training in chennai
java training in chennai velachery
Thank you for sharing this post.
ReplyDeleteArticle submission sites
Education
ReplyDeleteWow,great information. I am sure the info on your blog will help others,Thanks.
Spring Training in Chennai
Spring Training in Adyar
Spring Training in Velachery
Hibernate Training in Chennai
Hibernate Training
Spring Hibernate Training in Chennai
Spring Training in Tambaram
Spring Hibernate Training
This comment has been removed by a blog administrator.
ReplyDeleteExcellent information with unique content and it is very useful to know about the information based on blogs..
ReplyDeletePHP Training in Chennai
PHP Training in Bangalore
PHP Course in Bangalore
PHP Training Institute in Bangalore
PHP Classes in Bangalore
Spoken English Classes in Bangalore
Data Science Courses in Bangalore
AWS Training in Bangalore
DevOps Training in Bangalore
DOT NET Training in Bangalore
SAP QM Training In Noida
ReplyDeletemmorpg oyunlar
ReplyDeleteİnstagram takipci satın al
tiktok jeton hilesi
tiktok jeton hilesi
antalya saç ekimi
instagram takipci
İnstagram Takipçi Satın Al
Mt2 Pvp Serverler
instagram takipçi satın al
SMM PANEL
ReplyDeletesmm panel
HTTPS://İSİLANLARİBLOG.COM
İnstagram takipçi satın al
hirdavatciburada.com
https://www.beyazesyateknikservisi.com.tr
servis
tiktok jeton hilesi
uc satın al
ReplyDeletelisans satın al
nft nasıl alınır
en son çıkan perde modelleri
özel ambulans
minecraft premium
en son çıkan perde modelleri
yurtdışı kargo
Thank you for sharing this article. It has a lot of valuable information.
ReplyDeletehttps://espirittech.com/php-development/
Good content. You write beautiful things.
ReplyDeletevbet
korsan taksi
vbet
hacklink
hacklink
mrbahis
taksi
sportsbet
sportsbet
dijital kartvizit
ReplyDeletereferans kimliği nedir
binance referans kodu
referans kimliği nedir
bitcoin nasıl alınır
resimli magnet
NNQ
beşiktaş
ReplyDeleteeyüpsultan
gebze
marmaris
tekirdağ
K3M