Jun 7, 2012

PHP MYSQL - post to database

Hello googlers
On todays post mi gonna tell you how to post to mysql database.

First of all we must make the html form for index.html:
<html>
<body>
<form action="index.php" method="get">
<input type="text" name="user">
<input type="submit" value="post">
</form>
</body>
</html>
This html script will post form data to index.php, for example we write "Mikel" on the box and clock post button, the url will change to index.php?user=mikel

Now the php part, index.php:
<?php
// we get submitted data and check if user really submitted it
if(isset($_GET['user']){
$user = $_GET['user'];

// make an mysql connection, make sure to edit this
mysql_connect("host", "user", "password"); // host = host name, user = database user, password = database password
mysql_select_db("database"); // database = mysql database name

// escape input from dangerous charachters
$user = htmlentities(mysql_real_escape_string($user));
}
else{
// else print "you have not submitted anything
die("You have not submitted anything.");
}
?>

Ok we are done with html and php part, now go to mysql: localhost/phpmyadmin if you are on localhost, go to your database and create a table named users with 2 fields:
And click go.

And click save.
Now lets move back to index.php
After $user = htmlentities(mysql_real_escape_string($user)); create a new line and add:
mysql_query("INSERT INTO users(name) VALUES('$user');
echo "Submitted.";

Thats it! Whole script of index.php should look:

<?php
// we get submitted data and check if user really submitted it
if(isset($_GET['user']){
$user = $_GET['user'];

// make an mysql connection, make sure to edit this
mysql_connect("host", "user", "password"); // host = host name, user = database user, password = database password
mysql_select_db("database"); // database = mysql database name

// escape input from dangerous charachters
$user = htmlentities(mysql_real_escape_string($user));

mysql_query("INSERT INTO users(name) VALUES('$user');
echo "Submitted."; 

}
else{
// else print "you have not submitted anything
die("You have not submitted anything.");
}
?>

Thanks for reading and dont forget to follow us or like on facebook :D

6 comments:

  1. Nice tutorial..i got very useful information from your website,here is also a way to findinsert into database php

    ReplyDelete
  2. Good information.I like the way of writing and presenting.Rarely will get this type of good article.You can share more information about PHP.The author did not use bad language and words so the readers can easily understand article.
    custom essay service

    ReplyDelete
  3. Very good info. I prefer the way of showcasing this. Rarely could possibly get this type of great content. Also check out this http://www.designrfix.com/design/winning-clients-stereotypical-world-design-development It is possible to discuss more info related to PHP.

    ReplyDelete
  4. How can I display echo messages to html page after successful transaction?

    ReplyDelete
  5. Very useful information! It really works. It`s just great!

    ReplyDelete