Hi,
I've got a database of players with columns playerID, playerName, playerType,playerEntry.
The way I have written the application playerID has to be unique and has to run from 1 to n (number of players in the database).
I've got most things working but realised that my AddPlayer and DeletePlayer routines will mess this up.
So what I did was to add a clean up part at the end of the AddPlayer routine (see below) to try to set all the playerIDs back to 1 to n.
Doesn't work.
Any help appreciated.
Thanks...
<?php
/ Program: AddPlayer.php
Desc: Adds new player to the database. A confirmation
screen is sent to the user.
/
if (@$POST['newbutton'] == "Cancel")
{
header("Location: ChoosePlayerCat.php");
}
$playerName = $POST['playerName'];
$newName = $_POST['newName'];
if ($playerName == "new")
{
if ($newName == "")
{
include("NewName_form.inc");
exit();
}
else
{
$playerName=trim($newName);
//$playerName=ucfirst(strtolower(strip_tags($playerName)));
}
}
?>
<html>
<head><title>Add Player</title></head>
<body>
<?php
include("misc.inc");
$connection = mysql_connect($host,$user,$password)
or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection)
or die ("Couldn't select database");
$query = "SELECT * FROM Player WHERE playerName='$playerName'";
$result = mysql_query($query)
or die ("Couldn't execute query.");
$nrow = mysql_num_rows($result);
if ($nrow > 0)
{
echo "<hr><b>This player is already in the database</b><hr>\n";
}
else
/ Clean the data /
{
$playerType = "Middle";
$playerEntry = "Not Entered";
$query = "INSERT INTO Player
(playerName,playerType,playerEntry) VALUES
('$playerName','$playerType','$playerEntry')";
$result = mysql_query($query)
or die ("Couldn't execute query.");
$playerID = mysql_insert_id();
//CLEAN UP THE DATABASE
$query = "SELECT * FROM Player";
$result = mysql_query($query)
or die ("Couldn't execute query.");
$nrow = mysql_num_rows($result); // number of rows in table
$i=0;
while ($row = mysql_fetch_array($result))
{
$i++;
extract($row);
$tempvar = $playerName;
$query2 = "UPDATE Player SET playerID='$i' WHERE playerName='tempvar'";
$result2 = mysql_query($query)
or die ("Couldn't execute query.");
}
// END OF CLEANUP DATABASE
echo "The following player has been added to the
Database:<br>
<ul>
<li>Player Name: $playerName
<li>Usual wanted Time Slot: $playerType
<li>status for next draw: $playerEntry";
}
echo "</ul>";
echo "<a href='ChoosePlayerName.php'>Add Another Player?</a>";
echo "<br>\n";
echo "<a href='ShowPlayers.php'>View All Players?</a>\n";
echo "<br>\n";
echo "<a href='welcome.php'>Main menu</a>\n";
?>
</body></html>