TUTORIAL: Using Simple Forms to INSERT data into SQL DB

  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

Introduction



Welcome to an introduction of using HTML and PHP to build a database in MySQL, there is a few stages to this first the form page as you can see below and under would be our PHP code. I have the MySQL connection settings set for localhost which will be needed to altered as needed and a database created either by connecting to MySQL via command line or using PHPMyAdmin.


Form page


Code: [ Select ]
<html>
<body>

<form action="inserttest.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
city: <input type="text" name="city">
<input type="submit">
</form>

</body>
</html>
  1. <html>
  2. <body>
  3. <form action="inserttest.php" method="post">
  4. Firstname: <input type="text" name="firstname">
  5. Lastname: <input type="text" name="lastname">
  6. city: <input type="text" name="city">
  7. <input type="submit">
  8. </form>
  9. </body>
  10. </html>

After reading this you should see how the HTML form uses the name function to hold the data and a input submit that uses 'action' or use this to POST the data into a PHP form.


MySQL


Code: [ Select ]
CREATE DATABASE test
CREATE TABLE people
(
  firstName  VARCHAR(25),
  lastName  VARCHAR(25),
  city    VARCHAR(30)
)
  1. CREATE DATABASE test
  2. CREATE TABLE people
  3. (
  4.   firstName  VARCHAR(25),
  5.   lastName  VARCHAR(25),
  6.   city    VARCHAR(30)
  7. )

Here we can see the simple database we have to insert into MySQL, First we create the database and then the table and within the table we want to make 3 columns for our $_POST data using VARCHAR and (25) CHAR limit.


php code


Code: [ Select ]
<html>
<head></head>
<?php
$con = mysql_connect("localhost","root","toor");
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }

mysql_select_db("test", $con);

$sql="INSERT INTO member (firstName, lastName, city)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[city]')";

if (!mysql_query($sql,$con))
 {
 die('Error: ' . mysql_error());
 }
echo "1 record added";

mysql_close($con);
?>
<body>
</body>
  1. <html>
  2. <head></head>
  3. <?php
  4. $con = mysql_connect("localhost","root","toor");
  5. if (!$con)
  6.  {
  7.  die('Could not connect: ' . mysql_error());
  8.  }
  9. mysql_select_db("test", $con);
  10. $sql="INSERT INTO member (firstName, lastName, city)
  11. VALUES
  12. ('$_POST[firstname]','$_POST[lastname]','$_POST[city]')";
  13. if (!mysql_query($sql,$con))
  14.  {
  15.  die('Error: ' . mysql_error());
  16.  }
  17. echo "1 record added";
  18. mysql_close($con);
  19. ?>
  20. <body>
  21. </body>


Now we look at our php code and we start off by making a connection to the MySQL server, there is 3 items that is needed to make the connection. First block is server location, most cases it will be "localhost". 2nd block is username and 3rd is password. The if statement gives a error function if there is a bad connection.

mysql_select_db("test", $con) = Selecting the database you want to connect and modify.

$sql="INSERT INTO member (firstName, lastName, city) = This will be your SQL command that will be used for the execution.

Following by the Values, in the html form that was used the values that was assigned are POSTed to the php form and this is where the data is taken from the HTML form to the PHP form as variables.
VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[city]')";

And finally we have the checking function to see if everything went good and echo a finishing statement.


By this Point



We should have a general understanding on how we can use HTML, PHP and MySQL together to build a database driven web site. With this in mind you should be able to rebuild into most basic forms, hope this comes in handy for someone working out how to start with SQL and PHP.

Any Questions or suggestions on how to add on to this please post up.

Hope this helps someone gain that next step.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Poly
  • Guru
  • Guru
  • User avatar
  • Posts: 1091
  • Loc: Same place you left me.

Post 3+ Months Ago

Good info Zealous. I would suggest adding to this tutorial, or continue on to a new one with a guide on how to secure this method from SQL injection and XSS.
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

Poly wrote:
Good info Zealous. I would suggest adding to this tutorial, or continue on to a new one with a guide on how to secure this method from SQL injection and XSS.

it is about that time of the month that i write something else so i mite look into that this week.

Post Information

  • Total Posts in this topic: 3 posts
  • Moderator: Tutorial Writers
  • Users browsing this forum: No registered users and 2 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.