Sensation Energy

Tutorials


Content


Advertising


Function 7

OOP PHP MySql



Id Name Email Year Actiona

OOP PHP MySql

OOP PHP Object Oriented Programming is a type of programming language which helps in building complex, reusable web applications. It also connects to the database.

Search (search.php)

Search allows us to search for words we have previously entered via the form.


<?php
require 'conn.php';;
$v = "SELECT * from users";
$sql = $konekcija->query($v);
$upit = $_GET['search'];
$min_length = 4;
if(strlen($search) >= $min_length){
?>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Year</th>
</tr>
</thead>
<tbody>
<?php
$va = "SELECT * FROM users
WHERE (`user_id` LIKE '%".$search."%') OR (`name` LIKE '%".$search."%') OR (`year` LIKE '%".$search."%') OR (`email` LIKE '%".$search."%')";
$t = $con->query($va);
if(mysqli_num_rows($t) > 0){ // if one or more rows are returned do following
while ($value = $t->fetch_array()) {
?>
<tr>
<td><?php echo $value['user_id']; ?></td>
<td><?php echo $value['name']; ?></td>
<td><?php echo $value['email']; ?></td>
<td><?php echo $value['year']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}else{
echo "No results";
}
}else{
echo "min_length ".$min_length;
}
?>

Search Form (search_form.php)

Search form is clasic form which is used to enter the keywords that we will search.


<form action="rezult.php" method="GET">
<input type="text" name="search" class="form-input"><br><br>
<input type="submit" value="Search" class="btn-7">
</form>

Database

Database a database containing a table with fields that we will use in our CRUD. it is necessary to create a database named CRUD 2021 in PHPMyAdmin. When you do that, a new page will open and in the upper tab you should click on another SQL field where a page will open in which you will insert the previously copied lower code. And that’s how you made the base.


CREATE TABLE `users` (
`user_id` int(20) NOT NULL,
`name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`year` int(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `users`
ADD PRIMARY KEY (`user_id`);
ALTER TABLE `users`
MODIFY `user_id` int(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=102;


Connect (conn.php)

Connect allows us to connect to the base.


<?php
$server = "localhost";
$user = "root";
$password = "root";
$base = "7_2021";
$con = mysqli_connect($server, $user, $password, $base);
?>

Select (index.php)

Select allows us to list all the data in the table. Here, all columns from the table are selected unconditionally and printed


<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</a></th>
<th>Year</th>
<th>Actiona</th>
</tr>
</thead>
<tbody>
<?php
require_once "conn.php";
$oop = $con->query("SELECT * FROM users");
foreach ($oop as $row) {
?>
<tr>
<td><?php echo $row['user_id']; ?></td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['year'];?></td>
<td><a href="view.php?user_id=<?php echo $row['user_id']; ?>">View</a>
<a href="edit.php?user_id=<?php echo $row['user_id']; ?>">Edit</a>
<a href="delete.php?user_id=<?php echo $row['user_id']; ?>">Delete></a></td>
</tr>
<?php
}
?>
</tbody>
</table>

Insert (add.php)

Insert allows us to insert data into the database. It is important that the name = "add" long and ame = "name" input from the form match if (isset ($ _ POST ['add'])) {and $ name = $ _POST ['name'] ;, insert jus those values we set.


<?php
require 'con.php';
if(isset($_POST['add'])){
$name = $_POST['name'];
$email = $_POST['email'];
$year = $_POST['year'];
$sql = "INSERT INTO users (name, email, year) VALUES ('{$name}', '{$email}','{$year}')";
if (mysqli_query($con, $sql)){
echo "<script>window.history.go(-2);</script>";
} else {
echo "Eror";
}
}
?>
<form action="" method="post">
<div class="form-group">
<label>Name:</label>
<input type="text" name="name" class="form-input" />
</div>
<div class="form-group">
<label>Email:</label>
<input type="text" name="email" class="form-input">
</div>
<div class="form-group">
<label>Year</label>
<input type="number" name="year" class="form-input">
</div>
<div class="btn-group">
<button type="submit" name="add">Add</button>
</div>
</form>


View (view.php)

The view allows us to see only the individual values from the table via id. From select.php, click View, and then go to the View.php page. It is important to emphasize that this ID is important if the value of id 1 shows all data related to 1.


<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Year</th>
</tr>
</thead>
<tbody>
<?php
require 'conn.php';
$user_id=$_GET['user_id'];
$upit= "SELECT * FROM users WHERE user_id='$user_id'";
$procedural = mysqli_query($con, $upit);
while($res = mysqli_fetch_array($procedural)){
$user_id = $res['user_id'];
$name = $res['name'];
$email = $res['email'];
$year = $res['year'];
}
?>
<tr>
<td><?php echo $user_id; ?></td>
<td><?php echo $name ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $year;?></td>
</tbody>
</table>

Edit (edit.php)

Edit lets we edit data from the table using ID. Also from selekt.php we chose edit.php and in the value of the ID it shows all the data related to that ID.


<?php
require 'conna.php';
$user_id = $_GET['user_id'];
$query = "SELECT * FROM users WHERE  user_id = $user_id";
$select= mysqli_query($con, $query);
while ($row = mysqli_fetch_assoc($select)) {
$user_id = $row['user_id'];
$name = $row['name'];
$email = $row['email'];
$year = $row['year'];
}
if (isset($_POST['edit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$year = $_POST['year'];$result = mysqli_query($con, "UPDATE users SET name='$name', year='$year', email='$email' WHERE user_id=$user_id");
echo "<script>window.history.go(-2);</script>";
}
?>
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="thread_title"><h2>Name</h2></label>
<input type="text" name="name" class="form-input" value="<?php echo $name; ?>">
</div>
<div class="form-group">
<label><h2>Year</h2></label>
<input type="text" class="form-input" name="year" value="<?php echo $year; ?>">
</div>
<div class="form-group">
<label><h2>Email</h2></label>
<input type="text" name="email" class="form-input" value="<?php echo $email; ?>">
</div>
<div class="btn-group">
<input type="submit" name="edit" value="Edit">
</div>
</form>

Delete (delete.php)

Delete allows us to delete data from the table. Of course via id. From select.php we delete and click delete and all values related to the ID we selected will be deleted.


<?php
require 'conn.php';
$user_id = $_GET['user_id'];
$sql = "DELETE FROM useres WHERE user_id=$user_id";
$query = mysqli_query($konekcija, $sql);
if ( $query) {
echo "<script>window.history.back();</script>";
}
?>