Accessing a MySQL Database via PhpMyAdmin - Tutorial 1

[PHP and My SQL]

In the example(database name is tutorial) we created a table name “student_info”.
This Table includes “Two Columns” These are “Name” and
“Address”.

To create a database and table open PHP MYADMIN.

::: Step 1:::

To create a database, give the name of database in the text box as we’ve(tutorial)
given on this text box. And click on create button.

::: Step 2:::

When you click on the create button you will see a new window.

::: Step 3:::

Now click on my SQL button to create a table on this database.

::: Step 4:::

Now write the query to create the table..

create table student_info(name varchar(80),address varchar(80));

Like this and click go…..

when you click on go button it will appear this window…

Now we return to PHP Code

The INSERT INTO statement is used to insert records in any table.

INSERT INTO TABLE_NAME (column1, column2,...)
  VALUES (value1, value2,...)

How to INSERT a record in a table through a HTML Form.

::: Step 1 ::: Take a Simple HTML Form

 

<html>
<body>
<form action="save.php" method="post">

Your Name: <input type="text" name="name" />
Your Address: <input type="text" name="address" />

<input type="submit"  Value= “Save My Information”>
</form>
</body>
</html>

Action is used to give the name of the next page. With the help of action we can
retrieve the values from current page to next page.

The Action of this Form is save.php when the user click the submit button then
all the values of the form will go on save.php.

The Coding of save.php is given below…….

::: Step 2 ::: save.php

<?php
$link= mysql_connect("localhost","root","");

if (!$link) die (DB_ERR_DOC);

mysql_select_db("tutorial")
	or die("Could not select database");

$name=& $_POST[‘name’];
$address=& $_POST[‘address’];

$result=mysql_query("INSERT INTO student_info (name, address)
VALUES ('$name', '$address ',)");

If($result)
{
echo “Information Saved”;
}
else
{
echo “Information Not Saved”;
}
?>

This is coding for database connectivity.

<?php
$link= mysql_connect("localhost","root","");

if (!$link) die (DB_ERR_DOC);

mysql_select_db("tutorial")
	or die("Could not select database");

Explanation:-

1. localhost:- localhost is machine/computer name which the user used for creating
projects.
2. Database Name:- At this place you can use give your project name.

This is coding to get the last page values to this page.

$name = & $_POST[‘name’];
$address = & $_POST[‘address’];

Explanation:-

1. Variables:- These are the variables. That stores the values from the last
page.
2. Functions:- With the help of POST function we retrieve the values of text
boxes from previous page.
3. Name of Text Boxes:- These are the name of text boxes of HTML page which
we need to retrieve in this page.

This is syntax and in this syntax we can run all queries of Mysql.

$result=mysql_query("INSERT INTO student_info (name, address)
VALUES ('$name', '$address ',)");

1. Syntax:- This is syntax under which we can run any query of mysql.
2. Query:- It is INSERT INTO query. this used insert any record in database.

This coding is used here for confirmation that whether the records has been
saved or not.

If($result)
{
echo “Information Saved”;
}
else
{
echo “Information Not Saved”;
}

Which means if the record saved then give

Output= Information Saved.

Else

Output= Not Saved.

[End]

 

Tags: , , , , ,


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply