Database Connectivity in Php & Mysql

Login.php

<?php
session_start();
?>
<html>
<form>
<table border=2 height=20% width=20% align=center>
<tr>
<td colspan=2>Username:<input type="text" name="txtname">
        Password:<input type="password" name="txtpass"></td>
</tr>
<tr>
<td  colspan=2 align=center><input type="submit" name="btnlogin" value="Login"><br>
<a href="registration.php">create new account?</a></td>
</tr>
</table>
</form>
</html>
<?php
extract($_REQUEST);
if(isset($btnlogin))
{
    mysql_connect("localhost","student","") or die("connection failed...");   
    mysql_select_db("user") or die("database not found...");
    $result=mysql_query("select * from userdetail");   
    $flag=0;
    while($row=mysql_fetch_array($result))
    {
        if($row[0]==$txtname and $row[1]==$txtpass)
            $flag=1;   
    }
    if($flag==1)
    {
        $_SESSION['user']=$txtname;
        header("Location:home.php");       
    }
    else
        echo "Invalid User";
}
?>


Home.php 
<?php
session_start();
if(empty($_SESSION['user']))
{
    header("Location:login.php");
}
else
{
    $name=$_SESSION['user'];
    echo"welcome..".$name."<br>";
    mysql_connect("localhost","student","") or die("connection failed...");
    mysql_select_db("user") or die("database not found...");
    $result=mysql_query("select * from userdetail where username='$name'") or die("data not found..");
    $row=mysql_fetch_array($result);
    echo $row[0]."<br>";
    echo $row[1]."<br>";
    echo $row[2]."<br>";
    echo $row[3]."<br>";
    echo $row[4]."<br>";
    echo $row[5]."<br>";
    echo"<img src='images/$row[5]' height=10% width=10%>";
}
?>
<html>
<form>
<p align="right">
<input type="submit" name="btnsbmt" value="logout">
</form>
</html>
<?php
if(isset($_REQUEST['btnsbmt']))
{
    unset($_SESSION['user']);
    header("Location:login.php");
}
?>


Register.php

<html>
<form method="post" enctype="multipart/form-data">
<p align="right"><a href="search.php"><img src="search.jpg" HEIGHT="10%" WIDTH="10%"></a></p>
<table align=center>
<tr>
<td>Username:</td>
<td><input type="text" name="txtname" value=""></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="txtpass"></td>
</tr>
<tr>
<td>Gender:</td>
<td><input type="radio" name="rd" value="male">male
        <input type="radio" name="rd" value="female">female</td>
</tr>
<tr>
<td>City:</td>
<td><select name="lstcity">
        <option value="bhavnager">Bhavnagar</option>
        <option value="ahmedabad">Ahmedabad</option>
        <option value="surat">Surat</option>
        </select></td>
</tr>
<tr>
<td>Address:</td>
<td><textarea rows=4 cols=15 name="txtaAdd"></textarea> </td>
</tr>
<tr>
<td>Upload file:</td>
<td><input type="file" name="file1"></td>
</tr>
<tr>
<td><input type="submit" name="btnsbmt" value="SUBMIT"></td>



<td><input type="reset" name="btnclear" value="CLEAR"></td>
<td> <input type="submit" name="btnupdate" value="update"></td>

<td> <a href="delete.php"><img src="delete.jpg" HEIGHT="20%" WIDTH="20%"></a></td>

</tr>
</table>
</form>

<a href="login.php"><img src="back.jpg" HEIGHT="10%" WIDTH="10%"></a>
</html>
<?php
extract($_REQUEST);
if(isset($btnsbmt))
{
    mysql_connect("localhost","student","") or die("connection failed...");
    mysql_select_db("user") or die("database not found...");
    move_uploaded_file($_FILES['file1']['tmp_name'],"images/".$_FILES['file1']['name']);
    $img=$_FILES['file1']['name'];
    mysql_query("insert into userdetail value('$txtname','$txtpass','$rd','$lstcity','$txtaAdd','$img')") or die(mysql_error());
    echo"data inserted sucessfully...";
    display();
}

if(isset($btnupdate))
{
    mysql_connect("localhost","student","") or die("Connection failed...");
    mysql_select_db("user") or die("Database not found...");
    $photo=$_FILES['file1']['name'];
    move_uploaded_file($_FILES['file1']['tmp_name'],"images/".$_FILES['file1']['name']);
    mysql_query("UPDATE `userdetail` SET password='$txtpass',gender='$rd',city='$lstcity',address='$txtaAdd',photo='$photo' WHERE username='$txtname'") or die(mysql_error());
    echo "record updated successfully";
    mysql_close();
    display();
   
}

function display()
{

$result=mysql_query("select * from userdetail") or die(mysql_error());

   
    while($row=mysql_fetch_row($result))
    {
        echo "<table border=2 width=50%>";
        echo "<tr>";
        echo "<td align=center>".$row[0]."</td>";
        echo "<td align=center>".$row[1]."</td>";
        echo "<td align=center>".$row[2]."</td>";
        echo "<td align=center>".$row[3]."</td>";
        echo "<td align=center>".$row[4]."</td>";
        echo "<td align=center><img src='images/$row[5]' height=10% width=20%>"."</td>";
       
       
       

        echo "</tr>";
        echo "</table>";
    }
}
?>
 


Delete.php

<html>

<p align="right"><a href="login.php"><img src="back.jpg" HEIGHT="10%" WIDTH="10%"></a></p>
<form>

User name:<input type="text" name="txtname"><br>
<input type="submit" name="btnsbmt" value="DELETE">
</form>

</html>
<?php

extract($_REQUEST);
if(isset($btnsbmt))
{
    $name=$txtname;
    mysql_connect("localhost","student","") or die("connection failed...");
    mysql_select_db("user") or die("database not found...");
    mysql_query("delete  from userdetail where username='$name'") or die(mysql_error());
    echo"data delete sucessfully...";
    mysql_close();
   
}
?>


Search.php

<html>

<p align="right"><a href="login.php"><img src="back.jpg" HEIGHT="10%" WIDTH="10%"></a></p>
<form>

User name:<input type="text" name="txtname"><br>
<input type="submit" name="btnsbmt" value="search">
</form>

</html>
<?php
extract($_REQUEST);
if(isset($btnsbmt))
{
    $name=$txtname;
    mysql_connect("localhost","student","") or die("connection failed...");
    mysql_select_db("user") or die("database not found...");
    $result=mysql_query("select * from userdetail where username='$name' ") or die("mysql_error()");

    $row=mysql_fetch_row($result);
    echo "<img src='images/$row[5]' height=10% width=10%>"."<br>";
    echo "Name:".$row[0]."<br>";
    echo "Password:".$row[1]."<br>";
    echo "Gender:".$row[2]."<br>";
    echo "City:".$row[3]."<br>";
    echo "Address:".$row[4]."<br>";
}
?> 

 
Database Connectivity in Php & Mysql Database Connectivity in Php & Mysql Reviewed by Unknown on 7:41:00 PM Rating: 5

No comments:

Powered by Blogger.