All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Insert HTML Table Data Into Database Using PHP

Last Updated : Mar 11, 2024

How To Insert HTML Table Data Into Database Using PHP

In this tutorial we will show you the solution of how to insert HTML table data into database using PHP, generally most of the time we have done to insert HTML form data into a database but in this tutorial, we will understand how to insert data from a table.

So, without wasting our time let us start our today’s tutorial with the help of the example given below.

Step by step guide on how to insert HTML table data into database using PHP :-

Here, below we are going to show you how to insert HTML table data into the database using PHP.

For, this we are going to create a table in HTML and create some queries with help of PHP. Let us see the queries and table created with the help of the codes below.

<?php
$connection = mysqli_connect(“localhost”, “root”, “”,”database”)
$size=sizeof($_POST);
$number=$size/3;
for($i=1;$i<=$number;$i++)
{
$numberone="columnone".$i;
$first[$i]=$_POST[$numberone];
$numbertwo="columntwo".$i;
$second[$i]=$_POST[$numbertwo];
$numberthree="columnthree".$i;
$third[$i]=$_POST[$numberthree];
}
for($i=1;$i<=$number;$i++)
{
$query = "Insert into table_name (one,two,three) values ('$first[$i]','$second[$i]','$three[$i]');
if( mysqli_query($connection , $query))
{
echo “ Data Inserted Successfully”;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title> TalkersCode </title>
</head>
<body>
<form action="#" method="post">
<table border="2px" >
<tr>
<th>Column one</th>
<th> Column Two</th>
<th> Column Three </th>
</tr>
<?php
for($i=1;$i<=7;$i++)
{
?>
<tr>
<td>
<input type="text" name="columnone<?php echo $i; ?>" />
</td>
<td>
<input type="text" name=" columntwo<?php echo $i;?>" />
</td>
<td>
<input type="text" name=" columnthree<?php echo $i;?>" />
</td>
</tr>
<?php
}
?>
</table>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on the browser’s screen or display is always written inside these codes.
  5. Here, inside this we see that we create a table, and inside the table, we create some input tags in this to save or say get data. At last, a submit button is created to call PHP codes so that we can successfully able to save data into the database.
  6. Inside PHP codes, we get data from our table and use some queries to establish a connection to the database and table. At last, we save data to the database using the above codes. So, successfully we can save data from the HTML table into the database. For reference, you can see the above codes for better understanding.
  7. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last, in conclusion, here we can say that with the help of this article we can understand how to HTML table data into a database using PHP.

I hope this tutorial on how to insert HTML table data into database using PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪