<repo_name>farukkhan14/ClassicalPHP<file_sep>/details.php
<?php
session_start();

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("example") or die(mysql_error());

$ID = $_GET['id'];

$query = "SELECT * FROM students WHERE id='$ID'";
$result = mysql_query($query);
$data = mysql_fetch_object( $result );
?>

<!DOCTYPE html>
<html>
<head>
    <title>Update</title>
</head>
<body>
    <label for="id">ID: </label><?php echo $data->id ?>
    <br/>
    <label for="email">Email: </label><?php echo $data->email ?>
    <br/>
    <a href="index.php">Back to List</a>
</body>
</html><file_sep>/store.php
<?php
session_start();
/*
var_dump($_POST);

echo "<pre>";
print_r($_REQUEST);
print_r($_FILES);

echo "</pre>";

die();

*/


//collect the data
$email = $_POST["email"];

//validate
if(empty($_POST['email']) ){
    $message = "Email field cannot be empty";
    $_SESSION['message'] = $message;
    header('location:create.php');
    exit();
}

//sanitize data

$email = mysqli_real_escape_string($conn, $email);

//coonect to database
$conn = mysqli_connect("localhost", "example", "example","example") or die(mysqli_connect_error());

//prepare query
$query = "INSERT into students (email) values ('$email')";

//insert data
if (mysqli_query($conn, $query)) //if successfull
{
    $_SESSION['message']  = "Data is inserted successfully";
    header('location:index.php');
    exit();
}
else
{ //failed
    $_SESSION['message'] = mysqli_error($conn);
    header('location:create.php');
    exit();
}
?>

<file_sep>/edit.php
<?php
 
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("example") or die(mysql_error());
 
$ID = $_GET['id'];
 
$query = "SELECT * FROM students WHERE id='$ID'";
$result = mysql_query($query);
$data = mysql_fetch_object( $result );
?>
 
<!DOCTYPE html>
<html>
<head>
    <title>Update</title>
</head>
<body>
<form action="update.php" method="post">
    <label for="email">Email:</label>
    <input type="hidden" name="id" value="<?php echo $data->id?>">
    <input type="text" name="email" value="<?php echo $data->email ?>"/>
    <input type="submit" value="Update" name="update"/>
</form>
</body>
</html><file_sep>/delete.php
<?php
	session_start();
	mysql_connect("localhost", "root", "") or die(mysql_error());
	mysql_select_db("example") or die(mysql_error());
	
	$id= $_GET["id"];
	
	if(mysql_query("DELETE from students where id = $id"))
	
	{
	    $deleted = "Record deleted successfully";
        $_SESSION['deleted'] = $deleted;
        header('location:index.php');
	}
	
	<file_sep>/create.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
    <title>create</title>
</head>
<body>
<div class="message">
    <?php echo $_SESSION['message']; $_SESSION['message']=""; ?>
</div>
<form action="store.php" method="post" enctype="multipart/form-data">

    <label for="email">Email:</label>
    <input type="text" name="email" value="<EMAIL>" />

   <!--
    <input type="checkbox" name="hobby[]" value="Cycling" />Cycling
    <input type="checkbox" name="hobby[]" value="Singing" />Singing
    <input type="checkbox" name="hobby[]" value="Painting" />Painting


    <input type="radio" name="hobby" value="Cycling" checked="checked" />Cycling
    <input type="radio" name="hobby" value="Singing" />Singing
    <input type="radio" name="hobby" value="Painting" />Painting


    <select name="options">
        <option value="option1">Option 1</option>
        <option value="option2">Option 2</option>
        <option value="option3">Option 3</option>

    </select>


    <select name="multioptions[]" multiple="multiple">
        <option value="option1">Option 1</option>
        <option value="option2" selected="selected">Option 2</option>
        <option value="option3">Option 3</option>
        <option value="option4">Option 4</option>
        <option value="option5">Option 5</option>

    </select>

     <input type="file" name="aFile" />
    -->

    <input type="submit" value="Submit"/>
</form>

</body>
</html><file_sep>/index.php
<?php session_start();?>
<html>
    <head></head>
    <body>
    <?php if(isset($_SESSION['added'])){
        $message = $_SESSION['added'];
    ?>
    <div style="color: green"><?php echo $message;?></div>
    <?php 
    session_unset();
}?>

    <?php if(isset($_SESSION['deleted'])){
        $message = $_SESSION['deleted'];
    ?>
    <div style="color: red"><?php echo $message;?></div>
    <?php 
    session_unset();
    }?>
<table>
    <tr>
        <th>Serial</th>
        <th>Email</th>
        <th colspan="2">Actions</th>
    </tr>
    
	<?php
	
	mysql_connect("localhost", "root", "") or die(mysql_error());
	mysql_select_db("example") or die(mysql_error());
	$query= mysql_query("SELECT id,email from students");
    $i = 0;
	
	while($list=mysql_fetch_object($query)){
	
			echo "<tr>";
			echo"<td>".++$i."</td>";
			echo"<td>".$list->email."</td>";
			echo"<td>"."<a href='details.php?id=".$list->id."'><button>detail</button></a>"."</td>";
			echo"<td>"."<a href='edit.php?id=".$list->id."'><button>Edit</button></a>"."</td>";
			echo"<td>"."<a href='delete.php?id=".$list->id."'><button>Delete</button></a>"."</td>";
			echo "</tr>";
	}
	
	?>
	
	
</table>
<a href="create.php"><button>Create New</button></a>
        </body>
    </html><file_sep>/update.php
<?php
session_start();

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("example") or die(mysql_error());

if( isset( $_POST['update'] ) ){
    $id = $_POST['id'];
    $email = $_POST['email'];
    mysql_query("UPDATE students SET email = '$email' WHERE id = '$id'")
    or die(mysql_error());
     
    $message = "Record updated successfully";
    $_SESSION['added'] = $message;
    header('location:index.php');
    exit();
}
?>