[Solved] how to save data from textboxes to database on submit button click using php


The html code below is a snippet of how your form should look, though you have mentioned you already have this part done:

page1.html

<form method="POST" action="page2.php">
  <input type="text" name="usernameForm">
  <input type="password" name="passwordForm">
  <input type="submit" value="Submit">
</form>

The php code below then obtains the variables from page1.html after a user Submits their information from the form, and then inserts a line into a table in the database. When using this code, don’t forget to replace all the table names, columns, etc with the ones you have on your form, as well as the 4 “********” variables that give your code access to your database:

page2.php

//uses MySQL (PDO)
<?php
$usernameForm = $_POST['usernameForm'];
$passwordForm = $_POST['passwordForm'];

$servername = "********";
$username = "********";
$password = "********";
$myDB = "********";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$myDB", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 

    $sql = "INSERT INTO myTable (username, password) VALUES (:usernameForm, :passwordForm)";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':usernameForm', $usernameForm);
    $stmt->bindParam(':passwordForm', $passwordForm);
    $stmt->execute();

    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }

$conn = NULL;
?>

Edited: After having read your comment on wanting to have all this code on one page, a simple way of doing it is the following:

Replace this line:

<form method="POST" action="page2.php">

with this line:

<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">

What is happening here is the following:
PHP code is always run on the server. When you have your website fully loaded, your connection with the server terminated. Whatever information you are sending to the server via the <form>...</form> tags need to establish a connection with the server, which is why in general people user two pages (one for the input, and another one that receives the data), HOWEVER there is a simple and common way of keeping all this code in one page: have the <form>...</form> send to itself!

To do this you have 2 options:

  1. if your page was called page1.php, then you could just replace action="page2.php" with action="page1.php". However this in general is not good programming because if for some reason the name of your page changes, then your code breaks, which leads us to the second (and better) option.
  2. replacing action="page2.php" with action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>". What this does is almost the same as option 1, HOWEVER the php code inside action is asking the $_SERVER superglobal for what the name of the page is, hence preventing your page from “breaking” in cases like when you rename your file.

3

solved how to save data from textboxes to database on submit button click using php