All TalkersCode Topics

Follow TalkersCode On Social Media

PHP Form Example With Database

Last Updated : Jul 1, 2023

PHP Form Example With Database

In this article we will show you the solution of PHP form example with database, here we designed php form with two input fields and using post method we interacting with database and using isset() we executes database connection.

We needs to use mysqli_connect(),mysqli_query() methods in php for create database connection and inserting user entry on database table.

Step By Step Guide On PHP Form Example With Database :-

Here we defined form using html form with two input fields ‘Name, Password’ and a submit button.

When user clicks on submit button then user entered details inserted on database table with the help of name attribute and post method.

In php we defined isset() method for checking whether submit state is set or not then within that we defined database server details in mysqli_connect() method because it executes database connection i.e referred as $con variable and if they not executed then it throws error. Using mysqli_query() method we inserting all values in table ‘info’.

    if(!$con) echo "Failed";
    $qry=mysqli_query($con,"INSERT INTO info VALUES (' ','$n','$ps')");
    if($qry) echo "Inserted Successfully";
        <h1>PHP FORM</h1>
        <form method="post" action="">
        Name:<input type="text" name="name">
        Password:<input type="text" name="pass">
        <input type="submit" name="submit" value="Submit">
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here h1 tag for display heading ‘PHP FORM’ then we defined form tag with method attribute ‘post’ value which is must for insert form data to database.
  4. Within that we defined two input element with name attributes ‘name,pass’ which are important for access and store user inputs on database using php and submit button for generate process when user clicks on it.
  5. In php block we checking whether submit is set or not by isset() method and then we defined database server details for executes database connection using mysqli_connect() method then this connection referred by variable ‘$con’.
  6. Using if condition we checking whether database connection exists or not if they not present then it throws error on webpage.
  7. We accessing ‘name, password’ of user inputs by ‘$_POST[]’ property which is used to retrieve in html form any input fields by specifying name attributes value between square brackets. Likewise we gets ‘name, password’ and stored on variables ‘$n,$ps’.
  8. The mysqli_query() method will executes defined query of ‘INSERT INTO info VALUES (' ','$n','$ps')’ with $con variable so it will inserts user entered name and password on database ‘dbase’ table ‘info’ then result stored on variable ‘$qry’.
  9. Then using if condition we checked $qry returns whether true or not. If return true then we successfully inserted user data which is clarified by displaying this ‘Inserted Successfully’ message on webpage.

Conclusion :-

In conclusion now we are able to know how to use php form with database in php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser we can see form heading with two input fields user needs to fill input fields then when user clicks on submit button then success message ‘Inserted Successfully’ will display.

I hope this article on PHP form example with database 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 🡪