Understanding Sql

Does anyone know how do you create an html form that will send data to my cpanel sql database?

I’m sure there are a few people who know how to do this.
The first stage is to decide what fields you want in the form for the data you want to collect and the type of data you will accept for each filed.
Then create the html form with those fields, then a database table with corresponding columns, plus an ID column.
That’s the easiest bit. It’s not the html form that will insert the data to the database, the form processing script will do that part.
The script should first check if the form has been submitted. Then validate the data in each field, if there is any error, return to the form, if not, proceed and sanitise the data. Then run an sql query to insert the data.
How far did you get trying to do this?

1 Like

What kind of data you need to send into database ?

@SamA74 I just don’t know how to connect them.

@mlukac89 Data that will be name will be like name, address and a short note. Probably like 100 words. Any idea if you can help?

The best way is to take it one small step at a time. It may seem complex, it can help but think of complex things as just lots of very simple things in an arrangement. When you look at the simple things in isolation, they are not so daunting.
I already outlined the steps to take. If you take those one at a time, one will lead to the next until the system is complete.
The first step was:-

Looks like you are almost done with that one. That’s progress, soon you can start making the html form.

Got you. Can you help me with this so I can see what you did and learn from it? You don’t hav to do the entire thing. Just do sample input where it ask to enter a name and it will send the data to am sql database

You need to do it yourself.
Next make your html form.
This info may help with that:-
http://htmldog.com/guides/html/beginner/forms/
http://htmldog.com/guides/html/advanced/forms/

When you have your from, you can move to the next step.

First you need a html form with 3 input fields (name, address, short note and submit button).

After you create this next step is to link a form to a php file which will process your form data (action=“phpfile”).

Next is to create a php file which contain a connection to a database PDO and validate yours data from html form using post method.

If yours data are valid you can create a query which will insert yours data from form to database.

Btw you first need to create 4 fields in database, id, name, address, short_note, then you can connect to your database and insert, edit, delete your data.

Here is quick example :

<?php
//////////////////////////////// PDO connection to database ///////////////////////////////////

$db_host = 'localhost'; 	// host name
$db_user = 'root';			// database username
$db_pass = '';				// database password
$db_name = 'mydatabase';	// database name

$dbh = new PDO('mysql:host='.$db_host.';dbname='.$db_name.';charset=utf8', $db_user, $db_pass);

///////////////////////////////////////// end of connection ////////////////////////////////////

// error reporting to check if any errors
error_reporting(E_ALL);

// process form
// is submit button is pressed
if (isset($_POST['submit'])) {
	// check if other fields are not empty
	if (empty($_POST['name']) || empty($_POST['address']) || empty($_POST['short_note'])) {
		echo "All fiels are required."
	} else {
		// remove white space in front of inputs
		$name 		= trim($_POST['name']);
		$address 	= trim($_POST['address']);
		$note 		= trim($_POST['short_note']);

		// if fiels are not empty lets try to insert data into database
		// create query with prepared statement
		$query = $dbh->prepare("INSERT INTO table_name (name, address, short_note) VALUES (?, ?, ?)");
		// execute query
		$query->execute(array($name, $address, $note));

		// check if query is sucessfull and all data is inserted
		if ($dbh->lastInsertId()) {
			echo "Data inserted."; // data is inserted
		} else {
			echo "There was a problem while inserting your data, please try again or contact administrator."; // data is not inserted
		}

	}
}

?>

<!-- yours html form -->
<form action="proces-form.php" method="post">
	<input type="text" name="name" placeholder="Enter your name"><br>
	<input type="text" name="address" placeholder="Enter your address"><br>
	<textarea name="short_note"></textarea><br>
	<input type="submit" name="submit" value="Submit">
</form>

Do i have to create any table with sql in cpanel? Thank you.

You don’t create tables in cPanel, but you can create the database to contain the table(s) there if you want to.
The easiest way to create a table is probably in PHPMyAdmin, which you should be able to access via cPanel.

Thank you. But with the code you provided, do i have to create the database? Or will it automatically do it?

No, the database and table must be created first.
Google “how to make a database in phpmyadmin”.

If you have a database already created you can use CREATE TEABLE IF NOT EXISTS() to create table and fileds without accessing phpmyadmin.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.