Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ramvardhan's pull request #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 6 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,6 @@
# TEST

## This test includes multiple stages

## STAGE - 1

1. Create a table name details under database details with columns name, phone, email.
2. Create a php page called connect.php and write code to connect to the database.

## STAGE - 2

1. Create insert.php and include connect.php
2. Insert 5 rows of data using php.

## STAGE - 3

1. Create select.php
2. Select all data and print in a table format.
3. Select particular data using name constraint and print in table format.

## STAGE - 4

1. Create delete.php
2. Delete a row with email as a constraint.

## STAGE - 5

1. Create a webpage which includes buttons delete, insert, select.
2. The respect pages and their functionality should be done on click of each button.

## STAGE - 6

1. Use ajax call to send and recieve data between the frontend and the backend.

# Finally put a pull request to this repository once everything is done (also include the readme).


## Questions asked

1. Should we take data from user in task 2 to insert in table ? <br />
Ans : **YES**

2. What is ajax call?<br />
Ans : Google it you will find it.
# Ramvardhan - PHP Test Files

- Go to index.php.
- Even if you have no DB, it will prompt you and will create the DB and tables automatically.
- Then you can add the records.
- Ajax has been implemented to select the records when searching with name.
15 changes: 15 additions & 0 deletions connect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "details";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// echo "Connected successfully";
?>
64 changes: 64 additions & 0 deletions create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "details";

$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}

// Create database
$sql = "CREATE DATABASE ".$dbname;
if ($conn->query($sql) === TRUE)
{
echo "Database created successfully";
}
else
{
echo ("<script LANGUAGE='JavaScript'>
window.alert('Error creating database');
</script>");
echo "Error creating database: " . $conn->error;
}
$conn->close();

//Create tables

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
echo ("<script LANGUAGE='JavaScript'>
window.alert('Conn failed');
</script>");
die("Connection failed: " . $conn->connect_error);
}
else {
$sql = "CREATE TABLE details(
name VARCHAR(50),
phone VARCHAR(15),
email VARCHAR(50)
)";
if ($conn->query($sql) === TRUE)
{
echo "Table created successfully";
echo ("<script LANGUAGE='JavaScript'>
window.alert('Database and table has been created successfully');
window.location='./index.php';
</script>");
}
else
{
echo ("<script LANGUAGE='JavaScript'>
window.alert('Error creating table');
</script>");
echo "Error creating database: " . $conn->error;
}
$conn->close();
}

?>
19 changes: 19 additions & 0 deletions delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
include("./connect.php");
$delete_id=$_GET['del'];
$sql="DELETE FROM details WHERE email='$delete_id'";
if($conn->query($sql))
{
echo ("<script LANGUAGE='JavaScript'>
window.alert('The user has been deleted');
window.location='./index.php';
</script>");
}
else {
echo ("<script LANGUAGE='JavaScript'>
window.alert('Unknown error in deleting the user');
window.location='./index.php';
</script>");
}

?>
40 changes: 40 additions & 0 deletions getuser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>
<head>
<title>getuser.php</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</head>
<body>

<?php
$q = $_GET['q'];
include("./connect.php");
$sql="SELECT * FROM details WHERE name = '".$q."'";
$result = $conn->query($sql);
echo "<table class='table table-striped'>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Email</th>
<th></th>
</tr>";
while($row=$result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</td><td><a href=\"./delete.php?del=".$row["email"]."\"><button class='btn btn-danger btn-small'>Delete</button></a></td>";
echo "</tr>";
}
echo "</table>";
$conn->close();
?>
</body>
</html>
107 changes: 107 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "details";
$mysqli = new mysqli($servername,$username,$password);
$doesDBExist=$mysqli->select_db($dbname);
$mysqli->close();
if(!$doesDBExist)
{
echo ("<script LANGUAGE='JavaScript'>
window.alert('No DB found. Press OK to create DB and tables');
window.location='./create.php';
</script>");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Stage-6</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</head>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
<body>
<div class="container">
<br />
<h4><center>Records</center></h4>
<label class="mr-sm-2"><h5>Enter name to search:</h5></label>
<input type="text" class="form-control mb-2 mr-sm-2" name="name" onChange="showUser(this.value)">
<br />
<div id="txtHint"></div>
<br />
<h4><center>All Records</center></h4>
<br />
<?php
include("./connect.php");
$sql = "SELECT * FROM details";
$result = $conn->query($sql);
if($result) {
if($result->num_rows>0)
{
echo "<table class=\"table table-striped\">" .
"<tr><th>Name</th><th>Phone</th><th>Email</th><th></th></tr>";
while($row=$result->fetch_assoc())
{
echo "<tr><td>".$row["name"].
"</td><td>".$row["phone"].
"</td><td>".$row["email"].
"</td><td><a href=\"./delete.php?del=".$row["email"]."\"><button class='btn btn-danger btn-small'>Delete</button></a></td></tr>";
}
echo "</table>";
}
else {
echo "There are no records in the database";
}
}
else {
echo "There are no records in the database";
}
?>
<hr />

<h4>Enter values of new record to insert into database:</h4>
<br />
<form action="./insert.php" method="post">
<label class="mr-sm-2">Name:</label>
<input type="text" class="form-control mb-2 mr-sm-2" name="name">
<label class="mr-sm-2">Phone:</label>
<input type="number" class="form-control mb-2 mr-sm-2" name="phone">
<label class="mr-sm-2">Email:</label>
<input type="email" class="form-control mb-2 mr-sm-2" name="email">
<button type="submit" class="btn btn-success mb-2">Submit</button>
</form>
<br />

</div>
</body>
</html>
12 changes: 12 additions & 0 deletions insert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
include("./connect.php");
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$sql = "INSERT into details VALUES('".$name."','".$phone."','".$email."')";
if ($conn->query($sql) === TRUE)
echo ("<script LANGUAGE='JavaScript'>
window.alert('The record has been inserted successfully');
window.location='./index.php';
</script>");
?>
48 changes: 48 additions & 0 deletions select.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>
<head>
<title>details</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<br />
<h4>Records Found:</h4>
<br />
<?php
include("./connect.php");
$name = $_POST['name'];
$sql = "SELECT * FROM details WHERE name='".$name."'";
$result = $conn->query($sql);
if($result) {
if($result->num_rows>0)
{
echo "<table class=\"table table-striped\">" .
"<tr><th>Name</th><th>Phone</th><th>Email</th></tr>";
while($row=$result->fetch_assoc())
{
echo "<tr><td>".$row["name"]."</td><td>".$row["phone"]."</td><td>".$row["email"]."</td></tr>";
}
echo "</table>";
}
else {
echo "There is no record with the name ".$name;
}
}
else {
echo "There is no record with the name ".$name;
}

?>
<br />
<br />
<a href="./index.php"><button class="btn btn-primary">Go back</button></a>
</div>
</body>