Вы находитесь на странице: 1из 9

PHP (HYPERTEXT PREPROCESSOR) & MYSQL - LAMP STACK

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]

mysql> desc emp;


+----------------+-----------------+-------+-------+-----------+---------+
| Field

| Type

| Null | Key | Default | Extra |

+----------------+-----------------+-------+-------+-----------+---------+
| number

| int(11)

| NO | PRI | NULL

| name

| varchar(32) | NO |

| NULL

| designation | varchar(32) | NO |

| NULL

| department | varchar(32) | YES |

| 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

4 | tarun | administrator| management | 34567.00 |

| 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

| Null | Key | Default | Extra |

+--------------+----------------+-------+-------+-----------+--------+
| number

| int(11)

| NO | PRI | NULL |

| username | varchar(32) | NO |

| NULL |

| passwd

| NULL |

| varchar(20) | NO |

+--------------+----------------+-------+-------+-----------+--------+
3 rows in set (0.00 sec)

[9]

mysql> insert into user(number, username, passwd)values(2,'ravi','ravi123');


Query OK, 1 row affected (0.04 sec)
mysql> select * from user;
+-----------+---------------+-----------+
| number | username | passwd |
+-----------+---------------+-----------+
|

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]

echo "<td>" . $row['salary'] . "</td>";


echo "</tr>";
}
echo "</table>";
echo "like to update<a href=updating.php>click here</a><br>";
echo "like to insert more records<a href=insertform.php>click here</a><br>";
echo "delete?<a href=deleting.php>delete</a>";
mysql_close($con);
?>
Insertform.php
<html>
<body>
<form action="insert.php" method="post">
Employee name: <input type="text" name="name" />
Employee No: <input type="text" name="number" />
Department: <input type="text" name="department" />
Designation: <input type="text" name="designation" />
Salary: <input type="text" name="salary" />
<input type="submit" />
</form>
</body>
</html>
Insert.php
<?php
$con = mysql_connect("localhost","test","test123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("samples", $con);
$sql="INSERT INTO emp (name, number, designation, department, salary)
VALUES
('$_POST[name]','$_POST[number]','$_POST[designation]','$_POST[department]','$_POST[salary]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
echo( '<a href="selectdat.php">Click Me</a>' );
mysql_close($con);
?>

[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]

echo "salary :";


echo '<input name="salary" type="text" id="salary" value='.$row['salary'].'>';
echo '<br />';
echo '</p>';
echo '<p>';
echo '<input type="submit" name="Submit" value="Submit" >';
echo '</p>';
echo '</form>';
mysql_close();
?>
Update.php
<?php
$con = mysql_connect("localhost","test","test123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("samples", $con);
if($_POST['Submit']){
// Get parameters from form.
$number=$_POST['number'];
$name=$_POST['name'];
$designation=$_POST['designation'];
$department=$_POST['department'];
$salary=$_POST['salary'];
// Do update statement.
mysql_query("update emp set name='$name', designation='$designation', department='$department',
salary='$salary' where number='$number'");
echo "updated";
// Re-direct this page to select.php.
header("location:selectdat.php");
exit;
}
Deleting.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/>";
[14]

echo "Department :" .$row['department']. "<br/>";


echo "Salary :" .$row['saraly']. "<br>";
echo '<a href="deleteredirect.php?number='.$row['number'].'">delete</a>';
echo "<hr>";
}
mysql_close();
?>

[15]

Вам также может понравиться