• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

Adding records to a MySQL database using PHP

By Amrit Hallan | on Feb 23, 2004 | 0 Comment
MySQL Tutorials PHP Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

PHP and MySQL – Adding data

There comes time when your website tends to transcend the boundaries of mere brochure type looks. There is a need to interact, to seem dynamic, and to respond according to your visitor’s behavior. You also want your visitors to go through a list of offerings that run into hundreds. Creating hundreds of web pages having a uniform layout for hundreds of products can become an overwhelming task. Besides, it’s not easy to browse so many products sequentially — you need a mechanism to conduct searches or create sorted indexes.

A saintly combination of PHP and MySQL can come to your rescue. Once you have created a database with a well-defined structure, you can enter records and use those records as refence.

Here you can learn how to create an SQL database and its tables.

Once you have a database ready, you need a form to accept data, and then a php file to put that data into the MySQL table.

First the form. Assume we have a file with an online form named form.html. Here’s the form of the file:

<form name=”toSave” method=”post”

action=”save_it.php”>

<input type=”text” name=”name” size=”20″ /><br> <input type=”text” name=”email” size=”20″ /><br> <input type=”text” name=”city” size=”20″ /><br> </form>

Once we have this form ready, we need to create the php file it calls, namely, save_it.php. Suppose the name of the database is “visitors” and the table in this database is “visinfo” with fields “name”, “email”

and “city”.

<?php

$db=mysql_connect(“localhost”, “usrnm”, “pswd”) or die(“Could not connect to localhost.”); mysql_select_db(“visitors”, $db) or die(“Could not find visitors.”);

// The above lines establishes a connection with the // database. Keep localhost as is unless something different // is mentioned by your sql host. usrnm is user name and pswd is // password. What I want to say is, copy these lines as they are // and just replace the required fields and it should connect.

$querySQL = “insert into visinfo (d_name, d_email,

d_city) values ($name, $email, $city)”;

if(!$querySQL) error_message(sql_error());

// The above statement generates an error if you have setup the table in such a way that there should not be a duplicate entry.

?>

In my next article(s) I’ll show you how to query your database and then show the results on the web page.

Share this story:
  • tweet

Author Description

Amrit Hallan is a freelance web developer. You can follow the link below to checkout his website.

No Responses to “Adding records to a MySQL database using PHP”

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 1,240Followers 494Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    May 2013
    M T W T F S S
    « Oct    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.