Build CRUD Application PHP & Mysql. Remove information from application

Home » Tutorials » PHP+MySQL » Build CRUD Application PHP & Mysql. Remove information from application
In this lesson we’ll finish building web application. We only need to write function for delete data.
We will use similar approach while writing function for delete. We need not any form for delete.

Code lesson (players.php)

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Игроки</title>
	<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
	<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
	<header>
		<nav class="navbar navbar-default" role="navigation">
			<div class="container-fluid">
				<div class="navbar-header">
					<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
						<span class="sr-only">Toggle navigation</span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
					</button>
					<a class="navbar-brand" href="#">Sport CRM</a>
				</div>
		
				<div class="collapse navbar-collapse navbar-ex1-collapse">
					<ul class="nav navbar-nav">
						<li><a href="index.php">Главная</a></li>
						<li><a href="players.php">Игроки</a></li>
						<li><a href="teams.php">Команды</a></li>
						<li><a href="countries.php">Страны</a></li>
					</ul>
				</div>
			</div>
		</nav>
	</header>

	<div id="content">
		<div class="container-fluid">
			<?php include 'db.php'; ?>
			<?php include 'api.php'; ?>
			<?php
				$players = getAllPlayers($db);
			?>
			<table class="table table-bordered">
				<tr>
					<th>Игрок</th>
					<th>Команда</th>
					<th>Страна</th>
					<th>Удалить</th>
				</tr>
				<?php foreach ($players as $player) { ?>
					<tr>
						<td><a href="edit.php?player_id=<?php echo $player['player_id'];?>"><?php echo $player['player_name']; ?></a></td>
						<td><?php echo $player['team_name']; ?></td>
						<td><?php echo $player['country_name']; ?></td>
						<td><a class="btn btn-danger" href="delete.php?player_id=<?php echo $player['player_id'];?>">Удалить</a></td>
					</tr>
				<?php } ?>
			</table>

			<button id="addButton" class="btn btn-default">Добавить игрока</button>

			<form action="" method="POST" role="form" style="display: none; margin-top: 20px;">
		
			<div class="form-group">
				<label for="">Введите имя</label>
				<input type="text" class="form-control" id="name" name="name" placeholder="Введите имя">
			</div>

			<div class="form-group">
				<select name="country" class="form-control" id="country">
				<?php
					$countries = getAllCountries($db);
					foreach ($countries as $key => $value) {
						echo "<option value=".$value['country_id'].">".$value['country_name']."</option>";	
					}	
				?>
				</select>
			</div>
			
			<button type="submit" class="btn btn-default">Добавить</button>
		</form>
		</div>

		<?php
			if(isset($_POST['name']) && $_POST['name'] != '') {
				$name = $_POST['name'];
				$countryId = $_POST['country'];	
				addPlayer($db, $name, $countryId);
			}
			
		?>

	</div>

	<footer>
		
	</footer>

	<script>
		$("#addButton").click(function(){
			$("form").slideDown();
		});
	</script>
</body>
</html>

Code lesson (delete.php)

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Игроки</title>
	<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
	<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
	<header>
		<nav class="navbar navbar-default" role="navigation">
			<div class="container-fluid">
				<div class="navbar-header">
					<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
						<span class="sr-only">Toggle navigation</span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
					</button>
					<a class="navbar-brand" href="#">Sport CRM</a>
				</div>
		
				<div class="collapse navbar-collapse navbar-ex1-collapse">
					<ul class="nav navbar-nav">
						<li><a href="index.php">Главная</a></li>
						<li><a href="players.php">Игроки</a></li>
						<li><a href="teams.php">Команды</a></li>
						<li><a href="countries.php">Страны</a></li>
					</ul>
				</div>
			</div>
		</nav>
	</header>

	<div id="content">
		<div class="container-fluid">
			<?php include 'db.php'; ?>
			<?php include 'api.php'; ?>
			<?php
				$id = $_GET['player_id'];
				if($id) {
					deletePlayer($db, $id);	
				} else {
					echo "<h1>Error</h1>";
					exit();
				}
			?>
		</div>
	</div>

	<footer>
		
	</footer>
</body>
</html>

Code lesson (api.php)

function getAllPlayers($db) {
	$sql = "SELECT * FROM players
			LEFT JOIN teams ON players.team_id = teams.team_id
			LEFT JOIN countries ON players.country_id = countries.country_id;
	";
	$result = array();

	$stmt = $db->prepare($sql);

	$stmt->execute();

	while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
		$result[$row['player_id']] = $row;
	}

	return $result;
}

function getAllTeams($db) {
	$sql = "SELECT * FROM teams
			INNER JOIN countries ON teams.country_id = countries.country_id;
	";
	$result = array();

	$stmt = $db->prepare($sql);

	$stmt->execute();

	while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
		$result[$row['team_id']] = $row;
	}

	return $result;
}

function getPlayerById($db, $id) {
	$sql = "SELECT * FROM players
			WHERE player_id = :player_id
			";

	$stmt = $db->prepare($sql);
	$stmt->bindValue('player_id', $id, PDO::PARAM_INT);
	$stmt->execute();
	$row = $stmt->fetch(PDO::FETCH_ASSOC);
	return $row;
}

function savePlayer($db, $name, $id) {
	$sql = " UPDATE players
			 SET player_name = :player_name
			 WHERE player_id = :player_id
	";

	$stmt = $db->prepare($sql);
	$stmt->bindValue(':player_name', $name, PDO::PARAM_STR);
	$stmt->bindValue(':player_id', $id, PDO::PARAM_INT);

	$stmt->execute();
}


function getAllCountries($db) {
	$sql = "SELECT * FROM countries";
	$res = array();

	$stmt = $db->prepare($sql);

	$stmt->execute();

	while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
		$res[$row['country_id']] = $row;
	}

	return $res;
}

function addPlayer($db, $playerName, $countryId) {
	$sql = "INSERT INTO players(player_name, country_id) 
			VALUES(:player_name, :country_id)
	";

	$stmt = $db->prepare($sql);
	$stmt->bindValue(':player_name', $playerName, PDO::PARAM_STR);
	$stmt->bindValue(':country_id', $countryId, PDO::PARAM_INT);

	$stmt->execute();

}

function deletePlayer($db, $id) {
	$sql = "DELETE FROM players WHERE player_id = :player_id";

	$stmt = $db->prepare($sql);
	$stmt->bindValue(":player_id", $id, PDO::PARAM_INT);

	$stmt->execute();
}

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Pin It on Pinterest

Share This