Sensation Energy

Tutorials


Content


Advertising


Function 7

Procedural PHP MySql

Procedural PHP with Mysql Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.


Connect (conn.php)

Connect allows us to connect to the base. The code below shows what parameters we need and how it connects to the database. Database name, database username and password, and server address. Con allows us to tell in the following functions that this is the base.


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

Search form (search.html)

Search form is clasic form which is used to enter the keywords that we will search. The most important thing is here name="search" because the parameter we are looking for is.php recognizing that it is a form parameter we are looking for.


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

Search (search.php)

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


<?php
//Here first $ search = $ _ POST ['search']; recognize the name = "search" from the form
$search=$_POST['search'];
//Select is functin for sect from table users, and where condition serch user?_id or name, or email or year
$s = "select * from users where user_id LIKE '%$search%' OR name LIKE '%$search%' OR year LIKE '%$search%' OR email LIKE '%$search%' LIMIT 0 , 10";
$query = mysqli_query($con, $s);
if (!$results= mysqli_num_rows($query) == 0) {
while($results = mysqli_fetch_array($query)){
//while the loop prints everything from the select and where conditions
//this applies to the data contained in the database, if it does not exist it is Nothing found
?>
<tr>
<td data-title="id"><?php echo $results['user_id']; ?></td>
<td data-title="ime"><?php echo $results['ime']; ?></td>
<td data-title="email"><?php echo $results['email']; ?></td>
<td data-title="godina"><?php echo $results['godina']; ?></td>
</tr>
<?php
}
} else {
echo 'Nothing found';
}
?>



Clasic Procedural PHP MySql CRUD

Id Name Email Year Actiona

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;

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 'conn.php';
$sql = "SELECT * FROM users";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$user_id= $row['user_id'];
$name= $row['name'];
$email= $row['email'];
$year =  $row['year'];
?>
<tr>
<td><?php echo $user_id; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $yeara; ?></td>
<td><a href="view.php?user_id=<?php echo $user_id; ?>">View</a>
<a href="edit.php?user_id=<?php echo $user_id; ?>">Edit</a>
<a href="delete.php?user_id=<?php echo $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.


<form action="" method="post">
<div class="form-group">
<label for="thread_title">Name:</label>
<input type="text" name="name" placeholder="name"  class="form-input" />
</div>
<div class="form-group">
<label>Email:</label>
<input type="text" name="email" placeholder="email" class="form-input">
</div>
<div class="form-group">
<label for="datum">Year</label>
<input type="number" name="year" placeholder="year" class="form-input">
</div>
<div class="btn-group">
<button type="submit" name="add" class="btn-3" >POST</button>
</div>
</form>
<?php
require 'conn.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";
}
}
?>

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 scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Email</a></th>
<th scope="col">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 $ime; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $year; ?></td>
</tr>
<?php } ?>
</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 'conn.php';
$user_id = $_GET['user_id'];
$query = "SELECT * FROM users WHERE  user_id = $user_id";
$select_posts = mysqli_query($con, $query);
while ($row = mysqli_fetch_assoc($select_posts)) {
$user_id = $row['user_id'];
$name = $row['name'];
$year = $row['year'];
$email = $row['email'];
}
if (isset($_POST['edit'])) {
$name = $_POST['name'];
$year =  $_POST['year'];
$email = $_POST['email'];
$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><h2>Name</h2></label>
<input type="text" name="name" class="form-input" value="<?php echo $name; ?>">
</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="form-group">
<label><h2>Year</h2></label>
<input type="text" class="form-input" name="year" value="<?php echo $year; ?>">
</div>
<div class="btn-group">
<div align="center">
<input type="submit" class="btn-1" name="edit" value="Edit">
</div>
</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 users WHERE user_id=$user_id";
$query = mysqli_query($con, $sql);
if ( $query) {
echo "<script>window.history.back();</script>";
}
?>


Procedural with image PHP MySql

Id Name Image Actiona

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 `images` (
`user_id` int(20) NOT NULL,
`name` varchar(200) NOT NULL,
`image` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `images`
ADD PRIMARY KEY (`user_id`);
ALTER TABLE `images`
MODIFY `user_id` int(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=36;

Select (index.php)

Edit lets we edit data from the table using ID. The same as with the previous one, the display is with a image.


<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Image</th>
<th>Actiona</th>
</tr>
</thead>
<tbody>
<?php
require 'conn.php';
$sql = "SELECT * FROM images";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$user_id= $row['user_id'];
$name= $row['name'];
$image= $row['image'];
?>
<tr>
<td><?php echo $user_id; ?></td>
<td"><?php echo $name; ?></td>
<td><img src="images/<?php echo $image;  ?>" width="100px" height="100px" alt="<?php echo $image;?>"></td>
<td><a href="view.php?user_id=<?php echo $user_id; ?>">View</a>
<a href="edit.php?user_id=<?php echo $user_id; ?>">Edit</a>
<a href="delete.php?user_id=<?php echo $user_id; ?>">Delete</a></td>
</tr>
<?php } ?>
</table>

Insert (add.php)

Insert allows us to insert data into the database. We also insert an image here.


<form method="POST" action="" enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000">
<div class="form-group">
<label for="image">Image</label>
<input type="file" name="image" class="form-input">
</div>
<div class="form-group">
<label for="thread_title">Name:</label>
<input type="text" name="ime" placeholder="name"  class="form-input" />
</div>
<div class="btn-group">
<button type="submit" name="add" class="btn-3" >POST</button>
<input type="button" class="btn-2" onclick="location.href='index.php';" value="Cancel" />
</div>
</form>
<?php
require 'conn.php';
if(isset($_POST['add'])){
$image = $_FILES['image']['name'];
$target = "images/".basename($image);
$name = $_POST['name'];
$sql = "INSERT INTO images (image, nema VALUES('{$image}','{$name}')";
mysqli_query($con, $sql);
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
echo "<script>window.history.go(-2);</script>";
}else{
echo  "Error";
}
}
?>

View (view.php)

View allows us to see only individual values from the table prko id. The same as with the previous one, the display is with a image.


<table>
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Image</a></th>
</tr>
</thead>
<tbody>
<?php
require 'conn.php';
$user_id=$_GET['user_id'];
$upit= "SELECT * FROM images WHERE user_id='$user_id'";
$procedural = mysqli_query($con, $upit);
while($res = mysqli_fetch_array($procedural)){
$user_id = $res['user_id'];
$name = $res['name'];
$image= $res['image'];
?>
<tr>
<td><?php echo $user_id; ?></td>
<td><?php echo $name; ?></td>
<td><img src="images/<?php echo $image;  ?>" width="100px" height="100px" alt="<?php echo $image;?>"></td>
</tr>
<?php } ?><?php
require 'conn.php';
$user_id=$_GET['user_id'];
$upit= "SELECT * FROM images WHERE user_id='$user_id'";
$procedural = mysqli_query($con, $upit);
while($res = mysqli_fetch_array($procedural)){
$user_id = $res['user_id'];
$name = $res['name'];
$image= $res['image'];
?>
<tr>
<td><?php echo $user_id; ?></td>
<td><?php echo $name; ?></td>
<td><img src="images/<?php echo $image;  ?>" width="100px" height="100px" alt="<?php echo $image;?>"></td>
</tr>
<?php } ?>
</tbody>
</table>

Edit (edit.php)

Edit lets we edit data from the table using ID and with edit image.


<?php
require 'conn.php';
$user_id = $_GET['user_id'];
$query = "SELECT * FROM images WHERE  user_id = $user_id";
$select_posts = mysqli_query($con, $query);
while ($row = mysqli_fetch_assoc($select_posts)) {
$user_id = $row['user_id'];
$name= $row['name'];
$image= $row['image'];
}
if (isset($_POST['edit_post'])) {
$image = $_FILES['image']['name'];
$image_temp = $_FILES['image']['tmp_name'];
move_uploaded_file($image_temp, "images/$image");
$name = $_POST['name'];
result = mysqli_query($con, "UPDATE images SET image='$image', ime='$ime'  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 $ime; ?>">
</div>
<label for="image"><h2>Image</h2></label>
<div align="center">
<img width="20%" height="20%" src="images/<?php echo $image ?>">
</div>
</div>
<div class="form-group" action="">
<label for="image"><h2>New Image</h2></label>
<input type="file" name="image" class="form-input">
</div>
<div class="btn-group">
<div align="center">
<input type="submit" name="edit" value="Edit Post">
</div>
</div>
</form>

Delete (delete.php)

Delete allows us to delete data from the table via ID. With select.php we delete and click delete and all values associated with the ID we selected will be deleted.


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