Thursday, 2 March 2017

PHP cruds

php cruds operation

conn.php

<?php
    $host="localhost";
    $server="root";
    $password="";
    $db="crud";

    $conn=mysqli_connect($host,$server,$password,$db);

    if(!$conn)
    {
        echo "not connected";
    }
    else
    {
        echo "connected";
    }
?>

insert.php

<?php

    include("conn.php");

    if(isset($_POST['submit']))
    {
        $nm=$_POST['name'];
        $cty=$_POST['city'];
        $hby=implode(",",$_POST['hobby']);
        $gen=$_POST['gender'];
        $file=$_FILES['photo']['name'];

        $qry="insert into reg(name,city,hobby,gender,file) values('$nm','$cty','$hby','$gen','$file')";
        $res=mysqli_query($conn,$qry);

        move_uploaded_file($_FILES["photo"]["tmp_name"],$_FILES["photo"]["name"]);

        if($res)
        {
            echo "<script>
                    alert('inserted successfully');
                    window.location='display.php';
                </script>";
        }
    }
    if(isset($_POST['cancel']))
    {
        header("location:display.php");
    }

?>

<!DOCTYPE html>
<html>
<head>
    <title></title>

    <link rel="stylesheet"  href="bootstrap\css\bootstrap.css">
    <link rel="stylesheet"  href="bootstrap\css\bootstrap.min.css">

    </head>
<body>

<center>

    <form action="" method="post" name="frm1">
    <label>Name:</label>
    <input type="text" name="name">
    <br>

    <label>City:</label>
    <select name="city">
        <option>please select city</option>
        <option value="Dhoraji">Dhoraji</option>
        <option value="Jetpur">Jetpur</option>
        <option value="Junagadh">Junagadh</option>
        <option value="Ahmedabad">Ahmedabad</option>
    </select>
   
    <br>
    <label>Hobby:</label>
    <div class="checkbox">
    <input type="checkbox" name="hobby[]" value="playing">Playing
    <input type="checkbox" name="hobby[]" value="Watching">watching
    <input type="checkbox" name="hobby[]" value="gaming">gaming
    </div>
    <br>
    <label>Gender:</label>   
    <input type="radio" name="gender" value="male">Male
    <input type="radio" name="gender" value="female">Female
    <br>
    <label>File</label>
    <input type="file" name="photo" />
    <br/>
    <input type="submit" name="submit" value="submit" class="btn btn-info">
    <input type="submit" name="cancel" value="cancel" class="btn btn-info">
    <br><br>
    Show Records<a href="display.php"><font color="red"><b>&nbsp;Here</b></font> </a>
    </center>
    </form>

</body>
</html>

display.php

<?php
    include("conn.php");

    if (isset($_POST['search']))
    {
        $term=$_POST['term'];
        $qry1="SELECT * FROM reg WHERE name LIKE '%{$term}%' OR City LIKE '%{$term}%' OR gender LIKE '%{$term}%' OR hobby LIKE '%{$term}%'";
        $res=mysqli_query($conn,$qry1);
    }
    else
    {
        $qry = "select * from reg";
        $res = mysqli_query($conn,$qry);
    }
?>

<!DOCTYPE html>
<html>
<head>
    <title></title>

    <link rel="stylesheet"  href="bootstrap\css\bootstrap.css">
    <link rel="stylesheet"  href="bootstrap\css\bootstrap.min.css">
    <link rel="stylesheet"  href="bootstrap\css\bootstrap-flex.css">
    <link rel="stylesheet"  href="bootstrap\css\bootstrap-flex.min.css">
    <link rel="stylesheet"  href="bootstrap\css\bootstrap-grid.css">
    <link rel="stylesheet"  href="bootstrap\css\bootstrap-grid.min.css">
    <link rel="stylesheet"  href="bootstrap\css\bootstrap-reboot.css">
    <link rel="stylesheet"  href="bootstrap\css\bootstrap-reboot.min.css">

    <script src="bootstrap\js\bootstrap.js"></script>
    <script src="bootstrap\js\bootstrap.min.js"></script>

</head>
<body><br>
<div align="right">
    <form action="" method="POST">
        <input type="text" name="term" placeholder="Search Name">
        <input type="submit" name="search" value="search" class="btn btn-info">
        <input type="submit" value="Refresh" class="btn btn-success">
    </form>
</div><br>
<div class="container">
<div class="table-responsive col-md-12">
<table border="0" width="100" class="table table-hover table-border"> 

    <tr>
        <th>Name</th>   
        <th>City</th>
        <th>Hobby</th>
        <th>Gender</th>
        <th colspan="2">Operation</th>
    </tr>
    <?php
        while($row=mysqli_fetch_assoc($res))
        {
    ?>
    <tr>
        <td><?php echo $row['name']; ?></td>
        <td><?php echo $row['city']; ?></td>
        <td><?php echo $row['hobby']; ?></td>
        <td><?php echo $row['gender'] ;?></td>
        <td><a href="update.php?id=<?php echo $row['id']; ?>" class="btn btn-info"> <b> Update </b> </td>
        <td><a href="delete.php?id=<?php echo $row['id']; ?>" onclick="return confirm('are soure want to delete?;')" class="btn btn-danger"> <b> Delete </b> </td>
    </tr>

    <?php } ?>
    <tr>
        <td colspan="6" align="center">Register<a href="insert.php"><b>&nbsp;now</b></td></a></td>
    </tr>

</table>
</div>
</div>

</body>
</html>

delete.php

<?php

    include("conn.php");

    $id = $_GET['id'];

    $qry = "delete from reg where id='$id'";
    $res = mysqli_query($conn,$qry);

    if($qry)
    {
        echo "<script>window.location = 'display.php'</script>";
    }

?>

update.php

<?php

    include("conn.php");

    $id=$_GET['id'];
    $qry="select * from reg where id='$id'";
    $res=mysqli_query($conn,$qry);
    $row=mysqli_fetch_assoc($res);

    if(isset($_POST['submit']))
    {
        $nm=$_POST['name'];
        $cty=$_POST['city'];
        $hby=implode(",", $_POST['hobby']);
        $gen=$_POST['gender'];

        $updt="update reg set name='$nm', city='$cty', hobby='$hby', gender='$gen' where id='$id'";
        $res2=mysqli_query($conn,$updt);

        if($res2)
        {
            echo "<script>
                alert('recored updated');
                window.location='display.php';
            </script>";
        }
        else
        {
            echo "<script>
                alert('Please try again..');
                window.location='update.php';
            </script>";
        }
    }
    if(isset($_POST['cancel']))
    {
        echo "<script> window.location='display.php'; </script>";
    }

?>

<!DOCTYPE html>
<html>
<head>
    <title></title>

    <link rel="stylesheet"  href="bootstrap\css\bootstrap.css">
    <link rel="stylesheet"  href="bootstrap\css\bootstrap.min.css">
    </head>
<body>
<center>

    <form action="" method="post" name="frm1">
    <label>Name:</label>
    <input type="text" name="name" value="<?php echo $row['name']; ?>">
    <br>

    <label>City:</label>
    <select name="city">
        <option>please select city</option>
        <option value="Dhoraji" <?php if($row['city']=="Dhoraji") { echo "selected"; } ?>> Dhoraji</option>
        <option value="Jetpur" <?php if($row['city']=="Jetpur") { echo "selected"; } ?>>Jetpur</option>
        <option value="Junagadh" <?php if($row['city']=="Junagadh") { echo "selected"; } ?>>Junagadh</option>
        <option value="Ahmedabad" <?php if($row['city']=="Ahmedabad") { echo "selected"; } ?>>Ahmedabad</option>
    </select>
    <br>

    <?php
        $arr=$row['hobby'];
        $exp=explode(",",$arr);
    ?>

    <label>Hobby:</label>
    <div class="checkbox">
    <input <?php if(in_array('playing',$exp)) { echo "checked"; } ?> type="checkbox" name="hobby[]" value="playing">Playing
    <input <?php if(in_array('watching',$exp)) { echo "checked"; } ?> type="checkbox" name="hobby[]" value="watching">watching
    <input <?php if(in_array('gaming', $exp)) { echo "checked"; } ?> type="checkbox" name="hobby[]" value="gaming">gaming
    </div>
    <br>
    <label>Gender:</label>   
    <input <?php if($row['gender']=="male"){echo "checked"; } ?> type="radio" name="gender" value="male">Male
    <input <?php if($row['gender']=="female"){echo "checked"; } ?> type="radio" name="gender" value="female">Female
    <br>
    <input type="submit" name="submit" value="submit" class="btn btn-info">
    <input type="submit" name="cancel" value="cancel" class="btn btn-info">

</center>
</form>
</body>
</html>