Академический Документы
Профессиональный Документы
Культура Документы
EX NO : 2
DATE : 16.08.2012, 23.08.2012, 30.08.2012
AIM:
ALGORITHM:
[7]
MYSQL
Step 1:
[fosslab@fosslab ~]$ mysql -u root -h localhost -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.14 MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
=======================================================================================
Step 2:
mysql> create database samples;
Query OK, 1 row affected (0.00 sec)
mysql> create user 'test'@'localhost' identified by 'test123';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on samples.* to test;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+------------------------------+
| Database
+------------------------------+
| information_schema |
| mysql
| performance_schema |
| samples
| test
+------------------------------+
5 rows in set (0.03 sec)
mysql> use samples;
Database changed
mysql> create table if not exists emp(number int(11)not null, name varchar(32) not null, designation
varchar(32)not null,department varchar(32), salary double(8,2) not null, primary key(number));
Query OK, 0 rows affected (0.12 sec)
[8]
| Type
+----------------+-----------------+-------+-------+-----------+---------+
| number
| int(11)
| NO | PRI | NULL
| name
| varchar(32) | NO |
| NULL
| designation | varchar(32) | NO |
| NULL
| NULL
| salary
| NULL
| double(8,2) | NO |
+----------------+-----------------+-------+-------+-----------+---------+
5 rows in set (0.00 sec)
mysql> insert into emp(number, name, designation, department, salary)values
(2,'ravi','manager','marketing',40000.00), (3,'arnav','executive','management',45000.00),
(4,'tarun','administrator','management',34567.00);
Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from emp;
+-----------+----------+------------------+-------------------+--------------+
| number | name | designation | department | salary
+-----------+----------+------------------+--------------------+-------------+
|
2 | ravi
| manager
| marketing
| 40000.00 |
3 | arnav | executive
| management | 45000.00 |
+-----------+----------+------------------+--------------------+-------------+
3 rows in set (0.00 sec)
=======================================================================================
mysql> create table if not exists user(number int(11) not null, username varchar(32) not null, passwd
varchar(20) not null, primary key(number));
Query OK, 0 rows affected (0.12 sec)
mysql> desc user;
+--------------+----------------+-------+-------+-----------+--------+
| Field
| Type
+--------------+----------------+-------+-------+-----------+--------+
| number
| int(11)
| NO | PRI | NULL |
| username | varchar(32) | NO |
| NULL |
| passwd
| NULL |
| varchar(20) | NO |
+--------------+----------------+-------+-------+-----------+--------+
3 rows in set (0.00 sec)
[9]
2 | ravi
| ravi123 |
+-----------+---------------+-----------+
1 row in set (0.00 sec)
mysql> quit
Bye
PHP
Step 3:
Userlogin.php
<?php
echo '<html>';
echo '<head>';
echo '<title> Insert Title Here</title>';
echo '</head>';
echo '<body>';
echo '<form method=post action="userloginpass.php">';
echo '<table border=1>';
echo '<tr>';
echo '<td>';
echo '<b> user id</b>';
echo '</td>';
echo '<td><input type=text name=userid>';
echo '</tr>';
echo '<tr>';
echo '<td><b>password</b></td>';
echo '<td><input name=password type=password></input></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type=submit value=submit>';
echo '<td><input type=reset value=reset>';
echo '</tr>';
echo '</table>';
echo '</form>';
echo '</body>';
echo '</html>';
?>
[10]
Userloginpass.php
<?php
$usr=$_POST["userid"];
$pswd=$_POST["password"];
$con=mysql_connect("localhost","test","test123");
if(!con)
{
die('connection failed'.mysql_error());
}
mysql_select_db("samples",$con);
$result=mysql_query("select * from user");
while($row=mysql_fetch_array($result))
{
if($row["username"]==$usr&&$row["passwd"]==$pswd)
{echo "welcome<br>";
echo "<a href=selectdat.php>click here to view the employee details</a>";
}
else
{
echo "sorry";
}
}
?>
Selectdat.php
<?php
$con = mysql_connect("localhost","test","test123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("samples", $con);
$result = mysql_query("SELECT * FROM emp");
echo "<table border='1'>
<tr>
<th>Employee Name</th>
<th>Employee No</th>
<th>Department</th>
<th>Designation</th>
<th>Salary</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['number'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "<td>" . $row['designation'] . "</td>";
[11]
[12]
Updating.php
<?php
$con = mysql_connect("localhost","test","test123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("samples", $con);
$result=mysql_query("select * from emp");
while($row=mysql_fetch_assoc($result))
{
echo "Number :". $row['number']. "<br/>";
echo "Name :" .$row['name']. "<br/>";
echo "Designation :" .$row['designation']. "<br/>";
echo "Department :" .$row['department']. "<br/>";
echo "Salary :" .$row['saraly']. "<br>";
echo '<a href="updateredirect.php?number='.$row['number'].'">Update</a>';
echo "<hr>";
}
mysql_close();
?>
Updateredirect.php
<?php
$con = mysql_connect("localhost","test","test123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("samples", $con);
$number=$_GET['number'];
// Get records in all columns from table where column id equal in $id and put it in $result.
$result=mysql_query("select * from emp where number='$number'");
// Split records in $result by table rows and put them in $row.
$row=mysql_fetch_assoc($result);
// Close database connection.
echo '<form id="form1" name="form1" method="post" action="update.php">';
echo "Employee number :";
echo '<input name="number" type="text" id="number" value='.$row['number'].'>';
echo '<br />';
echo "Name :";
echo '<input name="name" type="text" id="name" value='.$row['name'].'>';
echo '<br />';
echo "Designation :";
echo '<input name="designation" type="text" id="designation" value='.$row['designation'].'>';
echo '<br />';
echo "Department :";
echo '<input name="department" type="text" id="department" value='.$row['department'].'>';
echo '<br/>';
[13]
[15]