Adding records to a MySQL database using PHP
By Amrit Hallan2004-02-23
PHP and MySQL - Adding data
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.
Tutorial pages:
|
|
|||||||||
You might also want to check these out:
|
Leave a Comment on "Adding records to a MySQL database using PHP"
You must be logged in to post a comment.
Link to This Tutorial Page!

