• Skip to primary navigation
  • Skip to content

Ammar Ali

What's tagline?

  • The Story Of Me
  • Facts About Me
  • The Blog
  • Got a Question?

Web Design / December 27, 2017

How To Insert & View Records In MySQL Using PHP

insert record into mysql using php

This post will help you learn the basics of how to insert records into MySQL using PHP and HTML forms as well as displaying records in table from MySQL using PHP. The code given below is simple to use and understand. You may also download the source files below.

The steps are as follow:

  1. Create a MySQL database using phpmyadmin
  2. Create HTML form to get data from user
  3. Use PHP to insert records into database
  4. View records in table using PHP/HTML

Create table

Open localhost/phpmyadmin, create new database of student and goto SQL and type in following MySQL command


CREATE TABLE student (
sid VARCHAR(100) PRIMARY KEY,
name VARCHAR(200) NOT NULL,
class VARCHAR(30) NOT NULL,
semester INT(10) NOT NULL
)

 

index.html

<html>
<body>

<h1>Insert new student record in mysql using PHP</h1>

<form action="insert.php" method="post">
SID: <input type="text" name="sid" /><br><br>
Name: <input type="text" name="name" /><br><br>
Class: <input type="text" name="myclass" /><br><br>
Semester: <input type="text" name="semester" /><br><br>
<input type="submit" value="Insert Record" />
</form>

</body>
</html>

 

insert.php


<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student";

$sid=$_POST[sid];
$name=$_POST[name];
$class=$_POST[myclass];
$semester=$_POST[semester];

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO student (sid, name, class, semester)
VALUES ('$sid', '$name', '$class', '$semester')";

// Confirmation
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

?>

 

view.php


<!DOCTYPE html>
<html>
<head>
<title>Student Records</title>
</head>

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "Select * from student";
$result = mysqli_query($conn, $sql);

echo "<table border='1'><tr><th>Id</th><th>name</th><th>class</th><th>semester</th>

</tr>";


while($row = mysqli_fetch_array($result))

{
echo "<tr>";
echo "<td>" . $row['sid'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "<td>" . $row['semester'] . "</td>";
echo "</tr>";
}

echo "</table>";

$conn->close();
?>

<body>
</body>
</html>

That’s it! Wasn’t that easy? 😉

Filed Under: Web Design

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

© 2021 · Ammar Ali