Skip to content

Instantly share code, notes, and snippets.

@exclusiveTanim
Last active July 15, 2020 09:23
Show Gist options
  • Save exclusiveTanim/021dcd7ff04461bbf84630ec2251112e to your computer and use it in GitHub Desktop.
Save exclusiveTanim/021dcd7ff04461bbf84630ec2251112e to your computer and use it in GitHub Desktop.
CRUD Operation PHP, MYSql
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Batch_103";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
?>
<?php
include "database.php";
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
$myusername = mysqli_real_escape_string($conn,$_POST['exampleInputEmail1']);
$mypassword = mysqli_real_escape_string($conn,$_POST['exampleInputPassword1']);
$sql = "SELECT id FROM user WHERE email = '$myusername' and pass = '$mypassword'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
//$active = $row['name'];
$count = mysqli_num_rows($result);
if($count == 1) {
//session_register("$myusername");
$_SESSION["login_user"] = $myusername;
echo "Session variables are set. and name is:". $_SESSION["login_user"];
header("location: home.php");
}else {
echo "Your Login Name or Password is invalid";
$error = "Your Login Name or Password is invalid";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>New Site</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1 style="text-align: center;">Login here</h1>
<div class="first_div">
<form action="" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" name="exampleInputEmail1"
id="exampleInputEmail1" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" name="exampleInputPassword1" id="exampleInputPassword1">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<div class="second_div">
<span class="d-flex justify-content-center pt-1"><label><b>Don't have a account?</b></label>
<a href="register.html"><button type="button" class="btn btn-info">Register Here</button></a></span>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>New Site</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1 style="text-align: center;">Registration Form</h1>
<div class="first_div">
<form action="register.php" method="post">
<div class="form-group">
<label for="userName">Enter Your Name</label>
<input type="text" class="form-control" name="userName" id="userName" aria-describedby="emailHelp">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" name="email1" id="exampleInputEmail1" aria-describedby="emailHelp">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" name="pass1" id="exampleInputPassword1">
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
</div>
</body>
</html>
<?php
$name = $_POST["userName"];
$email = $_POST["email1"];
$pass1 = $_POST["pass1"];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Batch_103";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO user (name, email, pass)
VALUES ('$name', '$email', '$pass1')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
header("Location: index.php");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
<?php
include('database.php');
session_start();
$user_check = $_SESSION['login_user'];
//echo "From check User :".$user_check;
//$ses_sql = mysqli_query($conn,"select name from user where name = '$user_check' ");
$sql = mysqli_query($conn,"SELECT name FROM user WHERE email='$user_check'");
$row = mysqli_fetch_array($sql,MYSQLI_ASSOC);
$login_session = $row['name'];
//echo "From Session PHP:".$login_session;
if(!isset($_SESSION['login_user'])){
header("location:index.php");
die();
}
?>
.first_div{
height: 450px;
width: 500px;
margin: 0 auto;
border: 2px solid green;
padding: 20px;
margin-top: 30px;
}
.second_div{
height: 50px;
width: 500px;
background-color: yellow;
margin: 0 auto;
border: 2px solid green;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment