Hi there
I’m trying to create a system in php where people can input times on F1 2011
And then compare them - Saves doing it over twitter!
Working through a book on building a database driven site by Kevin Yank
I’m working on the admin section where I can add/edit/delete players
The issue I’m having is I can’t seem to edit/delete players
I can add, no problem
I’ve followed the code from the book exactly (with my own content) but it just will not work!
I’ve attached screen shops of phpmyadmin as well
Live link is Manage Players
index.php
<?php
//ADD - WORKING
include '../../magic-quotes.php';
if (isset($_GET['add']))
{
$pagetitle = 'New player';
$action = 'addform';
$name ='';
$email ='';
$gamertag ='';
$console_format ='';
$id ='';
$button ='Add player';
include 'form.php';
exit();
}
if (isset($_GET['addform']))
{
include '../../connection.php';
$name = mysqli_real_escape_string ($link, $_POST['name']);
$email = mysqli_real_escape_string ($link, $_POST['email']);
$gamertag = mysqli_real_escape_string ($link, $_POST['gamertag']);
$console_format = mysqli_real_escape_string ($link, $_POST['console_format']);
$sql = "INSERT INTO player SET
name = '$name',
email = '$email',
gamertag = '$gamertag',
console_format = '$console_format'";
if (!mysqli_query($link, $sql))
{
$error = 'Error adding player';
include 'output.php';
exit();
}
header ('Location: .');
exit();
}
//EDIT
if (isset($_POST['action']) and $_POST['action'] == 'Edit')
{
include '../../connection.php';
$id = $mysqli_real_escape_string($link, $_POST['id']);
$sql = "SELECT id, name, email, gamertag, console_format FROM player WHERE id='$id'";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error fetching player details.';
include 'output.php';
exit();
}
$row = mysqli_fetch_array($result);
$pagetitle = 'Edit player';
$action = 'editform';
$name = $row['name'];
$email = $row['email'];
$gamertag = $row['gamertag'];
$console_format = $row['console_format'];
$id = $row['id'];
$button = 'Edit player';
include 'form.php';
exit();
}
if (isset($_GET['editform']))
{
include '../../connection.php';
$id = mysqli_real_escape_string ($link, $_POST['id']);
$name = mysqli_real_escape_string ($link, $_POST['name']);
$email = mysqli_real_escape_string ($link, $_POST['email']);
$gamertag = mysqli_real_escape_string ($link, $_POST['gamertag']);
$console_format = mysqli_real_escape_string ($link, $_POST['console_format']);
$sql = "UPDATE player SET
name = '$name',
email = '$email',
gamertag = '$gamertag',
console_format = '$console_format'
WHERE id='$id'";
if (!mysqli_query($link, $sql))
{
$error = 'Error editing player';
include 'output.php';
exit();
}
header ('Location: .');
exit();
}
//DELETE
if (isset($_POST['action']) and $_POST['action'] == 'Delete')
{
include '../../connection.php';
$id = mysqli_real_escape_string ($link, $_POST['id']);
//get times belonging to player
$sql = "SELECT id FROM times WHERE playerid='$id'";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error getting list of times to delete';
include 'output.php';
exit();
}
//for each time
while ($row = mysqli_fetch_array($result))
{
$timesId = $row[0];
//Delete times from tracks
$sql = "DELETE FROM tracktimes WHERE timesId='$timesid'";
if (!mysqli_query($link, $sql))
{
$error = 'Error deleting track entries for times.';
include 'output.php';
exit();
}
}
//Delete times belonging to player
$sql = "DELETE FROM times WHERE playerid ='$id'";
if (!mysqli_query($link, $sql))
{
$error = 'Error delete times for player';
include 'output.php';
exit();
}
//delete the player
$sql = "DELETE FROM player WHERE id ='$id'";
if (!mysqli_query($link, $sql))
{
$error = 'Error deleting player';
include 'output.php';
exit();
}
header('Location: . ');
exit();
}
//Display players list - WORKING
include '../../connection.php';
$result = mysqli_query($link, 'SELECT id, name, email, gamertag, console_format FROM player');
if(!result)
{
$error = 'Error fetching players from database.';
include 'output.php';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$players[] = array ('name' => $row['name'], 'email' => $row['email'], 'gamertag' => $row['gamertag'], 'console_format' => $row['console_format']);
}
include 'players.php';
?>
form.php
<?php include '../../helpers.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><?php htmlout($pagetitle); ?></title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8"/>
</head>
<body>
<h1><?php htmlout($pagetitle); ?></h1>
<form action="?<?php htmlout($action); ?>" method="post">
<div>
<label for="name">Name: <input type="text" name="name"
id="name" value="<?php htmlout($name); ?>"/></label>
</div>
<div>
<label for="email">Email: <input type="text" name="email"
id="email" value="<?php htmlout($email); ?>"/></label>
</div>
<div>
<label for="email">Gamertag: <input type="text" name="gamertag"
id="gamertag" value="<?php htmlout($gamertag); ?>"/></label>
</div>
<div>
<label for="email">console_format: <input type="text" name="console_format"
id="console_format" value="<?php htmlout($console_format); ?>"/></label>
</div>
<div>
<input type="hidden" name="id" value="<?php htmlout($id); ?>"/>
<input type="submit" value="<?php htmlout($button); ?>"/>
</div>
</form>
</body>
</html>
players.php
<?php include '../../helpers.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Manage Players</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8"/>
<link href="../../css/reset.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Manage Players</h1>
<p><a href="?add">Add new player</a></p>
<div class="header">Name</div><div class="header">Gamertag</div><div class="header">e-mail</div><div class="header">Console Format</div><div class="header">Action</div>
<br />
<br />
<ul>
<?php foreach ($players as $player): ?>
<li>
<form action="" method="post">
<div class="content">
<?php echo htmlspecialchars ($player['name'], ENT_QUOTES, 'UTF-8'); ?>
</div>
<div class="content">
<?php echo htmlspecialchars ($player['gamertag'], ENT_QUOTES, 'UTF-8'); ?>
</div>
<div class="content">
<?php echo htmlspecialchars ($player['email'], ENT_QUOTES, 'UTF-8'); ?>
</div>
<div class="content">
<?php echo htmlspecialchars ($player['console_format'], ENT_QUOTES, 'UTF-8'); ?>
</div>
<div class="content">
<input type="hidden" name="id" value="<?php echo htmlspecialchars ($player['id'], ENT_QUOTES, 'UTF-8'); ?>"/>
<input type="submit" name="action" value="Edit"/>
<input type="submit" name="action" value="Delete"/>
</div>
<br />
<br />
</form>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
Please help!
Thanks for reading
Any questions give me a shout