Sunday, 29 May 2016

Core PHP Insert Update View Delete

Code:

Main:

<html>
<form name="detail" action="insert.php" method="post" enctype="multipart/form-data">
<table>
<tr><td>Name</td><td><input type=text name=nm></td></tr>
<tr><td>Description</td><td><textarea name=desc></textarea></td></tr>
<tr><td>Price</td><td><input type=text name=price></td></tr>
<tr><td>Stock</td><td><input type=text name=stock></td></tr>
<tr><td>Date</td><td><input type=text name=dt value=<?php echo date('Y-m-d');?>></td></tr>
<tr><td>Hobbies</td><td><input type="checkbox" name="chk[]" value="cricket">cricket
                        <input type="checkbox" name="chk[]" value="chess">chess
                        <input type="checkbox" name="chk[]" value="football">football</td></tr>
<tr><td>Profile Picture</td><td><input type="file" name="photo"></td></tr>
<tr><td>Gender</td><td><input type=radio name=gender value=male>Male<input type=radio name=gender value=female>Female</td></tr>
<tr><td>Qualification</td><td><select name=qualification><option>MBA</option><option>MCA</option></select></td></tr>
<tr><td></td><td><input type=submit name=submit value=Insert></td>
</tr>
</table>
</form>
</html>

<form name="sort" method="post">
<input type=submit name=stock value=stock>
<input type=submit name=price value=price>
</form>

<body>
<table border=1>
<tr><th>Name</th>
    <th>Description</th>
    <th>Price</th>
    <th>Stock</th>
    <th>Date</th>
    <th>Profile Pic.</th>
    <th>Gender</th>
    <th>Qualification</th>
    <th colspan='2'>Action</th>
</tr>
<?php
$con=mysql_connect('localhost', 'root','');

$db=mysql_select_db("detail",$con);

$select="select * from detail";
$query=mysql_query($select);
if(isset($_POST['stock']))
{

$select="select * from detail order by stock desc";
$query=mysql_query($select);
}
if(isset($_POST['price']))
{

$select="select * from detail order by price";
$query=mysql_query($select);
}


while($row=mysql_fetch_array($query))
{
    echo "<tr>
                <td>$row[1]</td>
                <td>$row[2]</td>
                <td>$row[3]</td>
                <td>$row[4]</td>
                <td>$row[5]</td>
                <td><img src='data/$row[7]' height=50 width=50 /></td>
                <td>$row[8]</td>
                <td>$row[9]</td><td><a href='update.php?id=$row[0]'>Update</a></td><td><a href='delete.php?id=$row[0]'>Delete</a></td>
            </tr>";
}
?>
</table>
</body>


Insert :

 <?php

$con=mysql_connect('localhost', 'root','');
$db=mysql_select_db("detail",$con);

$nm=$_POST['nm'];
$desc=$_POST['desc'];
$price=$_POST['price'];
$stock=$_POST['stock'];
$gender=$_POST['gender'];
$qualification=$_POST['qualification'];
$dt=$_POST['dt'];
$photo=$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'],"data/".$_FILES['file']['name']);
$hobby=$_POST['chk'];
$hb=implode(",",$hobby);
$ins="insert into detail values('','$nm','$desc','$price','$stock','$dt','$hb','$photo','$gender','$qualification')";
$query=mysql_query($ins);

header("location:detail.php");

?>

Update :

<?php

$con=mysql_connect("localhost","root","");
$db=mysql_select_db("detail",$con);

$id=$_GET['id'];
//echo $id;
$sel="select * from detail where id=$id";
$query=mysql_query($sel);

$row=mysql_fetch_array($query);
echo mysql_error();

?>

<html>
<form name="detail" method="post" enctype="multipart/form-data">
<table>
<tr><td>Name</td><td><input type=text name=nm value=<?php echo $row[1]; ?>></td></tr>
<tr><td>Description</td><td><textarea name=desc ><?php echo $row[2]; ?></textarea></td></tr>
<tr><td>Price</td><td><input type=text name=price value=<?php echo $row[3]; ?>></td></tr>
<tr><td>Stock</td><td><input type=text name=stock value=<?php echo $row[4]; ?>></td></tr>
<tr><td>Date</td><td><input type=text name=dt value=<?php echo $row[5]; ?>></td></tr>
<?php
   
?>
<tr><td>Hobbies</td><td><input type="checkbox" name="chk[]" <?php $chk=explode(",",$row[6]); for($i=0;$i<count($chk);$i++) if($chk[$i]=='cricket') echo 'checked="checked"'; ?> value="cricket">cricket
                        <input type="checkbox" name="chk[]" <?php $chk=explode(",",$row[6]); for($i=0;$i<count($chk);$i++) if($chk[$i]=='chess') echo 'checked="checked"'; ?>value="chess">chess
                        <input type="checkbox" name="chk[]" <?php $chk=explode(",",$row[6]); for($i=0;$i<count($chk);$i++) if($chk[$i]=='football') echo 'checked="checked"'; ?> value="football">football</td></tr>
<tr><td>Profile Picture</td><td><input type="file" name="photo"></td></tr>
<tr><td></td><td><img src=data/<?php echo $row[7]; ?> height=100 width=100></td></tr>                       
<tr><td>Gender</td><td><input type=radio name=gender value=male <?php if($row[8]=='male') echo 'checked="checked"'; ?>>Male<input type=radio name=gender value=female <?php if($row[8]=='female') echo 'checked="checked"'; ?>>Female</td></tr>
<tr><td>Qualification</td><td><select name=qualification><option <?php if($row[9]=='MBA') echo 'selected="selected"'; ?> >MBA</option><option <?php if($row[9]=='MCA') echo 'selected="selected"';  ?> >MCA</option></select></td></tr>
<tr><td></td><td><input type=submit name=submit value=Update></td>
</tr>
</table>
</form>
</html>

<?php
error_reporting(0);
    $nm=$_POST['nm'];
    $desc=$_POST['desc'];
    $price=$_POST['price'];
    $stock=$_POST['stock'];
    $photo=$_FILES['photo']['name'];
move_uploaded_file($_FILES['photo']['tmp_name'],"data/".$_FILES['photo']['name']);
$date=$_POST['dt'];
    $hobby=$_POST['chk'];
$hb=implode(",",$hobby);
    $gender=$_POST['gender'];
    $qualification=$_POST['qualification'];
    $id=$_GET['id'];
    error_reporting(0);
    if(isset($_REQUEST['submit']))
    {
        //echo "hi";
        $up="update detail set name='$nm',description='$desc',price='$price',stock='$stock',photo='$photo',date='$date', hobby='$hb',gender='$gender',qualification='$qualification'  where id='$id'";
        $query=mysql_query($up);
        echo mysql_error();
        //header("location:detail.php");
        echo "<script>location.href='detail.php'</script>";
    }
   
?>



Delete:

<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db("detail",$con);
$id=$_GET['id'];
$del="delete from detail where id=$id";
mysql_query($del);
header("location:detail.php");
?>





 

SHARE THIS

Author:

0 comments: