answersLogoWhite

0


Best Answer

First create the form:

Now that we created the form it'll take it to frontPage.php. The "name" in the input tag is what the name of the $_POST is. So the username input would be $_POST['username']. It's POST because that is the method we used. We could also use GET(which would change to $_GET instead of $_POST)which shows the names of the inputs and what their value is. So say our username we typed in was foo$_POST['username'] would equal foo.

$username = $_POST['username'];

$password = $_POST['password'];

$username = mysql_escape_string($username);

$password = mysql_escape_string($password);

//Connect

$link = mysql_connect('mysql_host','mysql_username',mysql_password)

OR die(mysql_error());

//Connect to DB

$db = mysql_select_db('mysql_database',$link);

//Query To Insert Into Database

$mysql = "INSERT INTO db_table(username,password)

VALUES('$username','$password);

if(!mysql_query($mysql))

{

die(mysql_error());

}

?>

So now for what is going on. The $_POST['username'] and $_POST['password'] is the data from the form earlier. The $username = $_POST['username'] and $password = $_POST['password'] just makes it easier to type. Here comes the security. The mysql_real_escape_string puts a "" in front of any special characters that are in the string. symbols like <>'"%$ etc. gets a "" in front of it just in case someone is trying to inject Javascript or something into the input fields. It basically closes out symbols so nothing can be injected into the input fields.

Example:

Say we have an login form and we have $_POST['username'] and $_POST['password'] from the login and we do this code.

$username = $_POST['username'];

$password = $_POST['password'];

$mysql = 'SELECT * FROM db_table WHERE username = "$username" AND password = "$password"';

mysql_query($mysql);

if someone would put ' OR ''=' in the password field and foo in the username field the SELECT statement would become SELECT * FROM db_table WHERE username = 'foo' and password = 'OR ''=''. This is very very bad. This confuses mysql and the password comes back true and lets the person login without a valid password. So if there a username of foo the person would be able to login to their account. With mysql_real_escape_string it puts the "" in so it escapes that OR function so it would show up as ' OR ''=' and not return TRUE.

Now for the Query to the database. $mysql is the statment that inserts something into the table. So it's INSERT INTO db_table(whatever table you want to put it in)(username,password(what your categories are in your table)VALUES('$username',$password'(whatever your $_POST are for those categories))

The mysql_query($mysql) in the if statement is to put the data into the table. The if statment is very good to use when you're developing a website because what it does is it means if mysql_query($mysql) doesn't go into the table kill the rest of the script and display the error.

Now to get the data FROM the table, here is how you do it.

//Connect

$link = mysql_connect('mysql_host','mysql_username',mysql_password)

OR die(mysql_error());

//Connect to DB

$db = mysql_select_db('mysql_database',$link);

//The SELECT STATEMENT

$mysql = "SELECT * FROM db_table WHERE username = 'foo'";

//QUERY THE SELECT STATMENT

$query = mysql_query($mysql);

while($row = mysql_fetch_array($query,MYSQL_ASSOC))

{

echo $row['username'];

echo $row['password'];

}

I wont go over the SELECT and QUERY statement. The while loop is basically while this equals that do something. For this one it means while $row = mysql_fetch_array($query,MYSQL_ASSOC) do something.

The mysql_fetch_array returns the different rows from the table. So it fetches the $query and the MYSQL_ASSOC is so you can use the row names instead the number.

So the $row becomes a string with the mysql_fetch_arrayin it so it becomes $row['row name'].

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you insert data from a form to MySQL securely using PHP and display it back on another page?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is MySQL insert command?

The INSERT command in MySQL allows you to enter a new row of data to a table in your database.


What is the role of the insert command in MySQL?

The INSERT statement in MySQL is used to add data to a table. The INSERT INTO command inserts one or more rows into a MySQL table. For single row insertion you need to use the following syntax: INSERT Into table_name(column_1,column_2,column_3) VALUES(value_1, value_2, value_3); To speed up the process, you can use additional MySQL database management tools. For example, in dbForge Studio for MySQL, you can use the Generate Script as feature to insert one or more rows into a MySQL table.


How do you retrieve data from one MySQL table and create another MySQL table with the same data in it using PHP?

Execute a SELECT INTO statement on the mysql database: INSERT INTO destination_table (id, first_name, last_name) SELECT id, first_name, last_name from source_table;


How can i Have php post to self with mysql?

Mysql is an Database where Data from HTML forms will be inserted to it by some scripts like ASP 3 (classic), ASP.NET, PHP, ColdFusion and... What you need is a form (By HTML), an Database and table in Mysql and PHP to insert data from your form to the table. mysql insert command is: INSERT INTO table_name_here (column1, column2, ..) VALUES (value_of_column_1, value_of_column_2, ...)


What is an API Is MySQL an API?

MySQL is not an API MySQL is a database where you store data. An API is how you request information from another website.


How do you insert data to a MySQL database?

To insert data in Mysql you have two ways. 1. GUI Mysql Client 2. Command line tool GUI mysql client like PHPMyAdmin, Heidisql is helpful to enter the data in tables.Mysql also provide the command line tool for performing the Mysql operations. Please refer links.


What is an insert query in PHP and MySQL?

In a database table, the INSERT INTO statement is used to insert new rows. Let's create a SQL query with acceptable values using the INSERT INTO statement, and then run it by passing it to the PHP mysqli query() function to insert data into the table. To learn more about data science please visit- Learnbay.co


How do you fetch results in a column wise display using PHP and MySQL?

Hi, Use mysql_fetch_array($arrayname);


How to add data to a mysql database one line at a time?

Yes, use a INSERT statement. ex: INSERT into person_table (id, first_name, last_name) VALUES (1, 'Fred', 'Flintstone');


How do you copy a database of mysql in wamp server from one computer to another?

You have several options: * copy the database files from mysql/data/&lt;database_name&gt; to a new mysql Installation * use mysqldump utility to export the data from your database and load it into the new mysql installation * setup replication


How do you insert an image into MySQL using a BLOB?

There is a pretty solid tutorial here http://www.mysqltutorial.org/php-mysql-blob/ if you are using PDO. Without seeing your database table structure, it is hard to know what the propoer approach would be for your situation


How can i make a logging program and attach a database to it that when i insert a username and pass it goes on the database?

First create a form:Now create a page called login.php. The "name" field in the input is what goes to $_POST so the input named username is $_POST['username']. The value of the input is what the $_POST is.