Sunteți pe pagina 1din 39

PHP PROGRAMS

Created By: 2011041001100106 Nirmal Solanki

Sr no. 1

Programs Page No Create PHP pages to develop functionality in which User should be asked username, country name, and Favorite color. On the basis of users input, the Homepage of the selected country should be displayed with greeting message (Like good morning, Good afternoon or good evening) on it. Create PHP pages to develop following functionality Student registration login display profile on homepage Create PHP pages to develop functionality of insert, update, delete and display into product table. Columns in product table are Product (product_id, product_name, image, price, quantity, expire date). Here, image must be less than 50 kb and must be of .jpg format. Create PHP pages for creating image gallery. Include following functionalities in it Single image upload and Multiple image upload Display all images and display single image if user click on that image Add following functionality in problem 2 Allow student to select any 5 subjects out of given 7 subjects and store selected subject in database Display selected subjects of each Display welcome message on all pages Display header and footer on all pages Maintain session of user Logout facility

Program 1 Create PHP pages to develop functionality in which User should be asked username, country name, and Favorite color. On the basis of users input, the Homepage of the selected country should be displayed with greeting message (Like good morning, Good afternoon or good evening) on it. Home.php <html> <head> <title>Course</title> </head> <body> <?php session_start(); $tm = Date('H:i'); $tt = Date('H'); function tim($t) { $t=date("H"); if ($t<"10") { echo "Have a good morning!"; } else if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } } ?> <form action="logout.php" method="post"> <center> <h1>Welcome:<?php echo $_COOKIE['Unm'];?></h1> <?php echo "User Name= ".$_COOKIE['Unm']."</br>"; echo "Course Name= ".$_COOKIE['crs']."</br>"; echo "Fauvrit Font= ".$_COOKIE['fvfn']; ?>

</br> <font face="Agency FB"> Good Morning </font> </br> <font face="<?php echo $_COOKIE['fvfn'];?>"> <?php tim($tt); ?> </font> </br> <input type="submit" value="submit" name="submit"/> </br> </center> </form> </body> </html>

Index.php <html> <head> <title>Practical-2[set-2]</title> </head> <body> <form action="action.php" method="post"> <center> <h1>Select Course</h1> <table border="1" align="center"> <tr> <td>UserName</td> <td>:</td> <td><input type="text" name="txtName" /> </td> </tr> <tr> <td>Course Name</td> <td>:</td> <td> <select name="CNm"> <option value="India">India</option> <option value="RSA">RSA</option> <option value="UK">UK</option> <option value="USA">USA</option> </select>

</td> </tr> <tr> <td>Favorite Font</td> <td>:</td> <td> <select name="FvFotn"> <option value="Agency FB">Agency FB</option> <option value="Arial">Arial</option> <option value="Calibri">Calibri</option> <option value="Microsoft Himalaya">Microsoft Himalaya</option> </select> </td> </tr> <tr align="center"> <td colspan="3"><input type="Button" name="btnCancel" value="Cancel"/> <input type="submit" name="submit" Value="Submit" /></td> </tr> </table> </center> </form> </body> </html> India.php <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form action="logout.php" method="post"> <table border="1" width="100%" align="center" height="100%"> <tr align="center" height="25%"> <td colspan="3"><h1>WelCome......India</h1></td> </tr> <tr width="100%" align="center" height="65%"> <td colspan="1" align="left"> Home <br> <input type="submit" value="Logout" name="submit"/> </td> <td colspan="2"> <?php

session_start(); $tm = Date('H:i'); $tt = Date('H'); function tim($t) { $t=date("H"); if ($t<"10") { echo "Have a good morning!"; } else if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } } ?> <center> <h1>Welcome:<?php echo $_COOKIE['Unm'];?></h1> <?php echo "User Name= ".$_COOKIE['Unm']."</br>"; echo "Course Name= ".$_COOKIE['crs']."</br>"; echo "Fauvrit Font= ".$_COOKIE['fvfn']; ?> </br> <font face="<?php echo $_COOKIE['fvfn'];?>"> <?php tim($tt); ?> </font> </br> </center> </td> </tr> <tr width="100%" align="center" height="10%"> <td colspan="3"> Copyright 2013 by Vaibhav Patel. All Rights Reserved. </td> </tr> </table> </form> </body> </html>

RSA.php <html> <head> <title>Untitled Document</title> </head> <body> <form action="logout.php" method="post"> <table border="1" width="100%" align="center" height="100%"> <tr align="center" height="25%"> <td colspan="3">WelCome......RSA</td> </tr> <tr width="100%" align="center" height="65%"> <td colspan="1" align="left"> Home <br> <input type="submit" value="Logout" name="submit"/> </td> <td colspan="2"> <?php session_start(); $tm = Date('H:i'); $tt = Date('H'); function tim($t) { $t=date("H"); if ($t<"10") { echo "Have a good morning!"; } else if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } } ?> <center> <h1>Welcome:<?php echo $_COOKIE['Unm'];?></h1> <?php echo "User Name= ".$_COOKIE['Unm']."</br>"; echo "Course Name= ".$_COOKIE['crs']."</br>";

echo "Fauvrit Font= ".$_COOKIE['fvfn']; ?> </br> <font face="<?php echo $_COOKIE['fvfn'];?>"> <?php tim($tt); ?> </font> </br> </center> </td> </tr> <tr width="100%" align="center" height="10%"> <td colspan="3"> Copyright 2013 by Vaibhav Patel. All Rights Reserved. </td> </tr> </table> </form> </body> </html>

Uk.php <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled Document</title> </head> <body> <form action="logout.php" method="post"> <table border="1" width="100%" align="center" height="100%"> <tr align="center" height="25%"> <td colspan="3"><h1>WelCome......UK</h1></td> </tr> <tr width="100%" align="center" height="65%"> <td colspan="1" align="left"> Home <br> <input type="submit" value="Logout" name="submit"/> </td> <td colspan="2"> <?php

session_start(); $tm = Date('H:i'); $tt = Date('H'); function tim($t) { $t=date("H"); if ($t<"10") { echo "Have a good morning!"; } else if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } } ?> <center> <h1>Welcome:<?php echo $_COOKIE['Unm'];?></h1> <?php echo "User Name= ".$_COOKIE['Unm']."</br>"; echo "Course Name= ".$_COOKIE['crs']."</br>"; echo "Fauvrit Font= ".$_COOKIE['fvfn']; ?> </br> <font face="<?php echo $_COOKIE['fvfn'];?>"> <?php tim($tt); ?> </font> </br> </center> </td> </tr> <tr width="100%" align="center" height="10%"> <td colspan="3"> Copyright 2013 by Vaibhav Patel. All Rights Reserved. </td> </tr> </table> </form> </body> </html>

USA.php <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form action="logout.php" method="post"> <table border="1" width="100%" align="center" height="100%"> <tr align="center" height="25%"> <td colspan="3">WelCome......USA</td> </tr> <tr width="100%" align="center" height="65%"> <td colspan="1" align="left"> Home <br> <input type="submit" value="Logout" name="submit"/> </td> <td colspan="2"> <?php session_start(); $tm = Date('H:i'); $tt = Date('H'); function tim($t) { $t=date("H"); if ($t<"10") { echo "Have a good morning!"; } else if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } } ?> <center> <h1>Welcome:<?php echo $_COOKIE['Unm'];?></h1> <?php echo "User Name= ".$_COOKIE['Unm']."</br>"; echo "Course Name= ".$_COOKIE['crs']."</br>";

echo "Fauvrit Font= ".$_COOKIE['fvfn']; ?> </br> <font face="<?php echo $_COOKIE['fvfn'];?>"> <?php tim($tt); ?> </font> </br> </center> </td> </tr> <tr width="100%" align="center" height="10%"> <td colspan="3"> Copyright 2013 by Vaibhav Patel. All Rights Reserved. </td> </tr> </table> </form> </body> </html>

Logout.php <?php session_start(); session_destroy(); header('location:Ragister.php'); ?> Action.php <form action="Home.php" method="post"> <?php $UNm = $_POST['txtName']; $UCourse = $_POST['CNm']; $UFavFont = $_POST['FvFotn']; $Loc; $check = false; session_start(); setcookie("Unm", $UNm);

function selectedFont($fvfnt) { if($fvfnt == "Agency FB") { setcookie("fvfn", "Agency FB");//,time()+3600 } else if($fvfnt == "Calibri") { //$_SESSION['ffn']="Calibri"; setcookie("fvfn", "Calibri");//,time()+3600 } else if($fvfnt == "Arial") { //$_SESSION['fvfn']="Arial"; setcookie("fvfn", "Arial"); } else if($fvfnt == "Microsoft Himalaya") { //$_SESSION['fvfn']="Microsoft Himalaya"; setcookie("fvfn", "Microsoft Himalaya"); } } if($UCourse == 'USA') { $ff = selectedFont($UFavFont); setcookie("crs", "USA"); header('location:USA.php'); exit; } if($UCourse == 'UK') { $ff = selectedFont($UFavFont); //$_SESSION['crs']="UK"; setcookie("crs", "UK"); header('location:UK.php'); exit(); } else if($UCourse == 'RSA') { $ff = selectedFont($UFavFont); //$_SESSION['crs']="RSA"; setcookie("crs", "RSA");

header('location:RSA.php'); exit; } else if($UCourse == 'India') { $ff = selectedFont($UFavFont); //$_SESSION['crs']="India"; setcookie("crs", "India"); header('location:India.php'); exit; } else{ echo "do nothing"; } ?> </form>

Program 2 Create PHP pages to develop following functionality Student registration login display profile on homepage

index.php <html> <body> <br> <?php if (isset($_GET['c']) && $_GET['c'] == 0) { echo "<fieldset>"; echo "<p style='color: green'>Successfully registered...</p>"; echo "</fieldset><br>"; } if (isset($_GET['c']) && $_GET['c'] == 1) {

echo "<fieldset>"; echo "<p style='color: red'>Error in registration...</p>"; echo "</fieldset><br>"; } if (isset($_GET['l']) && $_GET['l'] == 1) { echo "<fieldset>"; echo "<p style='color: red'>Invalid Username or Password</p>"; echo "</fieldset><br>"; } ?> <fieldset> <legend style="color: blue"> Register Student</legend> <form action="register_Student.php" method="POST"> <table> <tbody> <tr> <td>Username</td> <td>:</td> <td><input type="text" name="txtUsername" value="" required/></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input type="password" name="txtPassword" value="" required/></td> </tr> <tr> <td>Full name</td> <td>:</td> <td><input type="text" name="txtName" value="" required/></td> </tr> <tr> <td>Course</td> <td>:</td> <td><input type="text" name="txtCourse" value="" required/></td> </tr> <tr> <td>Contact no.</td>

<td>:</td> <td><input type="text" name="txtContact" value="" required/></td> </tr> <tr> <td></td> <td></td> <td><input type="submit" value="Register" name="btnRegister" /></td> </tr> </tbody> </table> </form> </fieldset> <br> <br> <fieldset> <legend style="color: blue"> Login</legend> <form action="login.php" method="POST"> <table> <tbody> <tr> <td>Username</td> <td>:</td> <td><input type="text" name="txtUsernameLog" value="" required/></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input type="password" name="txtPasswordLog" value="" required/></td> </tr> <tr> <td></td> <td></td> <td><input type="submit" value="Login" name="btnLogin" /></td> </tr> </tbody> </table> </form>

</fieldset> </body> </html>

Register_Student.php <?php mysql_connect('localhost','root','root'); mysql_select_db('codeigniter'); $user=$_POST['txtUsername']; $pass=$_POST['txtPassword']; $name=$_POST['txtName']; $course=$_POST['txtCourse']; $co=$_POST['txtContact']; if(mysql_query("insert into tblStudent values('".$user."','".$pass."','".$name."','".$course."','".$co."')")) { header('Location:index.php?c=0'); } else { header('Location:index.php?c=1'); } ?>

Login.php

<html><body> <form action="logout.php" method="POST"> <div align="right"> <input type="submit" value="Logout" name="btnLogout" /></td> </div> </form>

</body> <?php mysql_connect('localhost','root','root'); mysql_select_db('codeigniter'); $user = $_POST['txtUsernameLog']; $pass = $_POST['txtPasswordLog']; $rs = mysql_query("select * from tblStudent where username='" . $user . "' and password='" . $pass . "'"); if (mysql_num_rows($rs) > 0) { session_start(); $_SESSION['username'] = $_POST['txtUsernameLog']; $ds = mysql_query("select * from tblStudent"); $obj = mysql_fetch_object($ds); echo "<br>"; echo "<fieldset>"; echo "Name : " . $obj->name; echo "<br>"; echo "Course : " . $obj->course; echo "<br>"; echo "Contact no : " . $obj->contact; echo "</fieldset>"; } else { header('Location:index.php?l=1'); } ?> </html>

Logout.php <?php session_start(); session_destroy();

header('Location:index.php'); ?>

Program 3 Create PHP pages to develop functionality of insert, update, delete and display into product table. Columns in product table are Product (product_id, product_name, image, price, quantity, expire date). Here, image must be less than 50 kb and must be of .jpg format. Add_rec.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Add Employee</title> </head> <body> <?php include('Head.php'); ?> <form method="post" action="phpInsert.php" name="frm"> <center> <h1>Insert Employee</h1> <table border="1" align="center"> <tr align="left"> <td>Eployee Name</td> <td>:</td> <td> <input type="text" name="txtName" /> </td> </tr> <tr align="left"> <td>Image</td> <td>:</td> <td> <input type="file" name="txtimg" /> </td> </tr> <tr align="left"> <td>Price</td>

<td>:</td> <td> <input type="text" name="txtPrice" /> </td> </tr> <tr align="left"> <td>qty</td> <td>:</td> <td> <input type="text" name="txtDesi" /> </td> </tr> <tr align="left"> <td>expire date</td> <td>:</td> <td> <input type="text" name="edate" /> </td> </tr> <tr align="center"> <td colspan="3"><input type="Button" name="btnCancel" value="Cancel"/> <input type="submit" name="submit" Value="submit"/></td> </tr> </table> </center> </form> <?php include('footr.php'); ?> </body> </html>

Connection.php <?php $host = 'localhost'; $dbName = 'demo'; $userName = 'root'; $password = ''; $connect = mysql_connect($host, $userName, $password) or die(mysql_error()); mysql_select_db($dbName) or die(mysql_error());

?>

Header.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-88591" /> <title></title> </head> <body> <table border="1" width="100%" align="center" height="100%"> <tr align="center" height="25%"> <td colspan="3"><img src="../Practicals[1]/image/srimca.jpg" width="100%" height="100%"/></td> </tr> <tr align="center"> <td><a href="../Practicals[2]/add-rec.php">Add Employee</a></td> <td><a href="../Practicals[2]/rec-display.php">Display Employee</a></td> <td><a href="../Practicals[2]/logout.php">Logout</a></td> </tr> </table> </body> </html>

Footer.php <table border="1" width="100%" align="center" height="100%"> <tr align="center"> <td> Copyright 2013 by veBs. All Rights Reserved. </td> </tr> </table>

Index.php

<html> <head> <title>Login Page</title> </head> <body> <form method="post" name="frm" action="phpLogin.php"> <center> <table border="1" width="100%" align="center" height="25%"> <tr align="center"> <td colspan="3"><h1>Product</h1></td> </tr> </table> </br> </br> </br> <table style="border:medium;border-style:inset" > <tr align="center"> <td colspan="2">Employee Login</td> </tr> <tr> <td>Username</td> <td><input type="text" name="username" id="username" value=""/></td> </tr> <tr> <td>password</td> <td><input type="password" name="password" id="password" value=""/></td> </tr> <tr align="center"> <td colspan="2"><input type="submit" name="submit" value="submit" onClick="return validation();"/> <input type="reset" name="reset" value="reset" /></td> </tr> </table> </br> </br> </br> <table border="1" width="100%" align="center" > <tr align="center"> <td>

Reserved. </tr> </table> </center>

Copyright 2013 by veBs. All Rights </td>

</form> </body>

</html>

<script language="javascript"> function validation() { if(document.getElementById('username').value == '') { alert('Please enter the username'); return false; } if(document.getElementById('password').value == '') { alert('Please enter the password'); return false; } return true; } </script>

phpAction.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-88591" /> <title>Employee Detail</title> </head> <body> <?php session_start(); include('connection.php'); $uname = $_POST['txtName'];

$password = $_POST['Month'.'Day'.'Years']; if($_POST) { $_SESSION['username'] = $rs[1]; header('location:rec-display.php'); } ?> <form method="post" action="" name="frm"> <center> <h1>Employee Detail's</h1> <table border="1"> <tr align="left"> <td>Eployee Name</td> <td>:</td> <td> <input type="text" name="txtName" /> </td> </tr> <tr align="left"> <td>Date Of Birth</td> <td>:</td> <td> <select name="Month"> <option value="1">Jan</option> <option value="2">Feb</option> <option value="3">March</option> <option value="4">April</option> </select> <select name="Day"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> <select name="Years"> <option value="1">1990</option> <option value="2">1991</option> <option value="3">1992</option> <option value="4">1993</option> </select> </td> </tr> <tr align="left"> <td>Designation</td>

<td>:</td> <td> <input type="text" name="txtDesi" /> </td> </tr> <tr align="left"> <td>Resume File[.pdf]</td> <td>:</td> <td> <input type="file" name="flResume" /> </td> </tr> <tr align="center"> <td colspan="3"><input type="Button" name="btnCancel" value="Cancel"/> <input type="submit" name="submit" Value="Submit" onclick="form.action='phpAction.php'"/></td> </tr> </table> </center> </form> </body> </html>

Delete.php <?php

include('connection.php'); $del = $_POST['chkDelete']; //echo $del; $result = mysql_query("delete from employee where employee_id='$del'"); //header('locarion:rec-display.php'); header('location:add-rec.php'); ?>

Insert.php <?php if($_POST) {

?>

$sql = "INSERT INTO `demo`.`product` (`product_name`, `image`, `price`, `quantity`, `expire_date`) VALUES ('".$_POST['txtName']."','".$_POST['txtimg']."',".$_POST['txtPrice'].",".$_POS T['txtDesi'].",'".$_POST['edate']."')"; $result = mysql_query($sql); //order executes if($result) { header('location:rec-display.php'); } else{ header('location:add-rec.php'); } } else { echo "erroe"; //header('location:add-rec.php'); }

phpLogin.php <?php session_start(); include('connection.php'); $uname = $_POST['username']; $password = $_POST['password']; if($_POST) { $query = "select * from user where username='$uname' and password='$password'"; $result=mysql_query($query); $rs = mysql_fetch_row($result); if(is_array($rs) && count($rs) > 0) { /*print "<pre>"; print_r($rs); die;*/ $_SESSION['username'] = $rs[1]; header('location:add-rec.php'); } else

{ } ?> }

header('location:index.php');

Rec_display.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-88591" /> <title>Untitled Document</title> </head> <body> <?php include('Head.php'); ?> <form method="post"> <center> <h1>Display Employee</h1> <table border="1"> <tr> <td>Delete</td> <td>EID</td> <td>EName</td> <td>EDOB</td> <td>EDesignation</td> <td>EResume</td> </tr> <?php include('connection.php'); $result = mysql_query("SELECT * FROM product"); while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td><input type='checkbox' name='chkDelete' value='$row[product_id]'/></td><td>"; echo $row['product_id']."</td>";

"<td>".$row['product_name']."</td>"; "<td>".$row['image']."</td>"; "<td>".$row['price']."</td>"; "<td>".$row['quantity']."</td>"; "<td>".$row['expire_date']."</td>"; } ?> <tr>

echo echo echo echo echo echo "</tr>";

<td colspan="6"><input type="submit" name="submit" value="Delete" onClick="form.action='phpDelete.php'"/> <input type="submit" name="submit" value="Update" onClick="form.action=''"/> <input type="submit" name="submit" value="Display" onClick="form.action=''"/> </td> </tr> </table> </center> </form> <?php include('footr.php'); ?> </body> </html>

Logout.php <?php session_start(); //unset($_SESSION['username']); session_destroy(); header('location:index.php');

?>

Program 4 Create PHP pages for creating image gallery. Include following functionalities in it o Single image upload and Multiple image o upload o Display all images and display single image if user click on that image

connection.php <?php $host = 'localhost'; $dbName = 'demo'; $userName = 'root'; $password = ''; $connect = mysql_connect($host, $userName, $password) or die(mysql_error()); mysql_select_db($dbName) or die(mysql_error()); ?> Download.php <html> <head> <title>Download File From MySQL</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-88591"> </head> <body> <?php include 'connection.php'; //include 'library/opendb.php'; $query = "SELECT id, name FROM upload"; $result = mysql_query($query) or die('Error, query failed'); if(mysql_num_rows($result) == 0) { echo "Database is empty <br>";

} else { while(list($id, $name) = mysql_fetch_array($result)) { ?> <a href="download.php?id=<?php=$id;?>"><?php=$name;?>vaiabhv</a> <br> <?php } } //include 'library/closedb.php'; ?> <?php if(isset($_GET['id'])) { // if id is set then get the file with the id from database //include 'library/config.php'; //include 'library/opendb.php'; $id = $_GET['id']; $query = "SELECT name, type, size, content " . "FROM upload WHERE id = '$id'"; $result = mysql_query($query) or die('Error, query failed'); list($name, $type, $size, $content) = mysql_fetch_array($result); header("Content-length: $size"); header("Content-type: $type"); header("Content-Disposition: attachment; filename=$name"); echo $content; //include 'library/closedb.php'; exit; } ?> </body> </html>

Smfileupload.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-88591" /> <title>Untitled Document</title> </head> <body> <form method="post" enctype="multipart/form-data"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> <?php if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } include 'connection.php'; //include 'library/opendb.php'; $query = "INSERT INTO `upload`(`name`, `type`, `size`, `content`) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; mysql_query($query) or die('Error, query failed');

//include 'library/closedb.php'; echo "<br>File $fileName uploaded<br>"; } ?> </body> </html>

Program 5 Add following functionality in problem 2 Allow student to select any 5 subjects out of given 7 subjects and store selected subject in database Display selected subjects of each Display welcome message on all pages Display header and footer on all pages Maintain session of user Logout facility Login.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <?php if(isset($_POST['registration'])) { header("location:registration.php"); } else if(isset($_POST['submit'])) {

mysql_connect("localhost","root",""); mysql_select_db("pr3"); $query="select * from student where username='$_POST[uname]' and password='$_POST[password]'"; $result=mysql_query($query); //echo $query; if($res=mysql_fetch_row($result)) { session_start(); echo $res[0]; $_SESSION['uid']=$res[0]; header("location:home.php"); } else { $msg=true; } } ?> <form action="" name="f1" method="post"> <table width="573" border="1"> <tr> <th width="257" scope="row">&nbsp;User Name </th> <td width="300">&nbsp;<input type="text" name="uname"/></td> </tr> <tr> <th scope="row">&nbsp; Password</th> <td>&nbsp;<input type="password" name="password"/></td> </tr> <tr> <th scope="row"><input type="submit" name="submit" value="Login"/></th> <td>&nbsp; <input type="submit" name="registration" value="Registration"/></td> </tr> </table> </form> <?php

if(isset($msg))echo "invalid username or password";?> </body> </html> Registeration.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title></title> </head> <body> <p> <?php if(isset($_POST['save'])) { if(count($_POST['subject']) != 5) { $err_select=true; } elseif($_POST['password'] != $_POST['repassword']) { $err_password=true; } else { $ids=array(); $subject_add=""; foreach($_POST['subject'] as $val){ $ids[] = $val; } for($i=0;$i<4;$i++) { $subject_add=$subject_add.$ids[$i].", "; } //print_r($subject_add); //print_r($_POST['subject']); mysql_connect("localhost","root","");

mysql_select_db("pr3"); $result=mysql_query("insert into student(fname,lname,course,semester,birthdate,username,password,subjects) values('$_POST[fname]','$_POST[lname]','$_POST[course]','$_POST[semester]','$_POST[bi rthdate]','$_POST[uname]','$_POST[password]','$subject_add')"); if($result) { header("location:login.php"); } else { header("location:error.php"); }

} }

?> </p> <form action="<?php $_PHP_SELF ?>" method="post" name="f1"> <h1> Registration</h1> <table width="529" border="1"> <tr> <th width="255" scope="row">First Name</th> <td width="258"><input type="text" name="fname"/></td> </tr> <tr> <th scope="row">Last Name</th> <td><input type="text" name="lname"/></td> </tr> <tr> <th width="257" scope="row">&nbsp;User Name </th> <td width="300">&nbsp;<input type="text" name="uname"/></td> </tr> <tr> <th scope="row">&nbsp; Password</th> <td>&nbsp;<input type="password" name="password"/></td> </tr> <tr> <th scope="row">&nbsp; Re-Password</th> <td>&nbsp;<input type="password" name="repassword"/></td> </tr> <tr> <th scope="row">Course</th> <td><input type="text" name="course"/></td>

</tr> <tr> <th scope="row">Semester</th> <td><input type="text" name="semester"/></td> </tr> <tr> <th scope="row">Birth Date</th> <td><input type="text" name="birthdate"/></td> </tr> <tr> <th scope="row">Favourite Subject<br/> (Any five)</th> <td><input type="checkbox" name="subject[]" value="ASP.NET"/>ASP.NET <input type="checkbox" name="subject[]" value="VB.NET"/>VB.NET <input type="checkbox" name="subject[]" value="JAVA"/>JAVA<br/> <input type="checkbox" name="subject[]" value="PHP"/>PHP <input type="checkbox" name="subject[]" value="C"/>C <input type="checkbox" name="subject[]" value="C++"/>C++ <input type="checkbox" name="subject[]" value="ANDROID"/>ANDROID</td> </tr> <tr > <td > <div align="center"> <input type="submit" name="save"/> </div></td> <td > <div align="center"></div></td> </tr> </table> </form> <?php if (isset($err_select)) echo "Please select any five option"; if (isset($err_password)) echo "password and re-password not match";?> <br> <center> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>copyright@srimca2013 </p> </center> </body> </html>

Subjects.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title></title> </head> <body> <?php session_start(); if(isset($_SESSION['uid'])) $sno= $_SESSION['uid']; else header("location:login.php"); if(isset($_POST['logout'])) { echo "here"; session_destroy(); header("location:login.php"); } else { mysql_connect("localhost","root",""); mysql_select_db("pr3"); $query="select * from student where sno=$sno"; $result=mysql_query($query); //echo $query; if($res=mysql_fetch_row($result)) { $uname=$res[6]; $subject=$res[8]; }

} ?> <form action="<?php $_PHP_SELF ?>" method="post" name="f2"> <table width="966" border="2"> <tr> <td width="109"><a href="home.php"> Home </a></td> <td width="198"> <a href="subjects.php">My Selected Subject</a></td> <td width="114"> <?php echo "welcome,".$uname; ?></td> <td width="135"> <input type="submit" name="logout" value="logout"/></td> </tr> </table> <h1> Selected Subjects are:<br /> </h1> <h2> <?php echo $subject; ?> </h2> </form> <?php //}?> <br> <center> copyright@srimca2013 </center> </body> </html>

Home.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title></title> </head> <body> <?php session_start(); if(isset($_SESSION['uid'])) $sno= $_SESSION['uid']; else header("location:login.php"); if(isset($_POST['logout'])) { session_destroy(); header("location:login.php"); } else { mysql_connect("localhost","root",""); mysql_select_db("pr3"); $query="select * from student where sno=$sno"; $result=mysql_query($query); //echo $query; if($res=mysql_fetch_row($result)) { $fname=$res[1]; $lname=$res[2]; $course=$res[3]; $sem=$res[4]; $birth=$res[5]; $uname=$res[6]; }

} ?> <form action="<?php $_PHP_SELF ?>" method="post" name="f2"> <table width="966" border="2"> <tr> <td width="109"><a href="home.php"> Home </a></td> <td width="198"> <a href="subjects.php">My Selected Subject</a></td>

<td width="114"> <?php echo "welcome,".$uname; ?></td> <td width="135"> <input type="submit" name="logout" value="Logout"/></td> </tr> </table> <table width="590" border="1"> <tr> <th width="291" scope="row">First Name</th> <td width="283"><input type="text" name="fname" value="<?php echo $fname;?>"/></td> </tr> <tr> <th scope="row">Last Name</th> <td><input type="text" name="lname" value="<?php echo $lname;?>"/></td> </tr> <tr> <th scope="row">Course</th> <td><input type="text" name="course" value="<?php echo $course;?>"/></td> </tr> <tr> <th scope="row">Semester</th> <td><input type="text" name="semester" value="<?php echo $sem;?>"/></td> </tr> <tr> <th scope="row">Birth Date</th> <td><input type="text" name="birthdate" value="<?php echo $birth;?>"/></td> </tr> </table> </form> <?php //}?> <br> <center> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>copyright@srimca2013 </p> </center> </body> </html>

S-ar putea să vă placă și