user3205084 user3205084 - 1 year ago 90
PHP Question

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

i need to transfer the data from textboxes designed in HTML to Mysql database using php. I tried a lot but didnt finally if anyone has a working code already for it then please let me know.
i just have two textboxes named "username" and "password" and a submit button.
I have already created a db [dbname: userdemo, tablename: users] but no results. Please use OOPs concept not procedural.
Any kind of help will be appreciated!!!

Answer Source

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


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

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:


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

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

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

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

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 remain your file.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download