Here is your code, I have updated both the previous files so discard them and use the new one
functions.php
[php]<?php
function db_connect(){
/* Change the DB Connection variables according to your project */
$host = 'localhost';
$user = 'tanzeelniazi';
$pass = 'abc';
$db = 'phphelp';
$conn = @mysqli_connect($host, $user, $pass, $db);
if (mysqli_connect_errno()) {
die("Connection Failed");
}
return $conn;
}
function query_form_echo($conn, $id){
$query = "SELECT * FROM players WHERE id = {$id}";
$results = mysqli_query ($conn, $query);
if (mysqli_affected_rows($conn) == 0) {
die ("ID doesn't exist");
}
if ($results && mysqli_affected_rows($conn) == 1) {
while ($player = mysqli_fetch_assoc($results)){
$Name = $player['name'] ;
$Number = $player['number'] ;
$Position = $player['position'] ;
$Team = $player['team'] ;
}
$player_info = array(
'name' => $Name,
'number' => $Number,
'position' => $Position,
'team' => $Team
);
return $player_info;
}
}
function query_rec_eupdate($conn, $id, $name, $number,
$position, $team){
$query = "UPDATE players SET
name = '{$name}',
number = '{$number}',
position = '{$position}',
team = '{$team}'
WHERE
id = {$id}
";
$results = mysqli_query($conn, $query);
if (!$results){ die ("Can't update the record right now"); }
return NULL;
}
function query_table_echo($conn){
$query = "SELECT * FROM players";
$results = mysqli_query ($conn, $query);
if ($results) {
echo " <form method=\"post\" action=\"\">
<table border=\"1px\">
<tr>
<th>Name</th>
<th>Number</th>
<th>Position</th>
<th>Team</th>
<th colspan=\"2\">Options</th>
</tr>";
while ($player = mysqli_fetch_assoc($results)){
$id = $player['id'];
$name = $player['name'] ;
$number = $player['number'] ;
$position = $player['position'] ;
$team = $player['team'] ;
echo "
<tr>
<td>{$player['name']}</td>
<td>{$player['number']}</td>
<td>{$player['position']}</td>
<td>{$player['team']}</td>
";
echo "
<input type=\"hidden\" name=\"Name[]\"
value=\"{$name}\">
<input type=\"hidden\" name=\"ID[]\"
value=\"{$id}\">
<input type=\"hidden\" name=\"Team[]\"
value=\"{$team}\">
<td>
<input type=\"submit\" name=\"{$name}-{$id}\"
value=\"Edit\">
</td>
<td>
<input type=\"submit\" name=\"{$name}-{$id}\"
value=\"Delete\">
</td>
</tr>
";
}
echo " </table>
</form>
";
}
}
function query_rec_delete($conn, $id, $name){
$query = "DELETE FROM players
WHERE
id = {$id} AND name = '{$name}'
";
echo $query;
$results = mysqli_query($conn, $query);
if (!$results){ die ("Can't delete the record right now"); }
return NULL;
}
?>[/php]
record.php
[php] <?php
session_start();
require_once(‘functions.php’);
$conn = db_connect();
?>
Colorado Basketball Club
All Available Players
<?php
query_table_echo($conn);
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
if ($action = array_search('Edit', $_POST)){
list($_SESSION['name'], $_SESSION['id']) = explode('-', $action);
$_SESSION['name'] = str_replace("_", " ", $_SESSION['name']);
header("Location: update.php"); // Your (update.php) HTML code file. Change it if necessary.
} elseif ($action = array_search('Delete', $_POST)) {
list($_SESSION['name'], $_SESSION['id']) = explode('-', $action);
$_SESSION['name'] = str_replace("_", " ", $_SESSION['name']);
query_rec_delete($conn, $_SESSION['id'], $_SESSION['name']);
header("Location: record.php"); // Your (update.php) HTML code file. Change it if necessary.
}
else {
echo "Wrong Action Selected!";
}
}
?>
<?php
mysqli_close($conn);
?>
[/php]
update.php
[php]<?php
session_start();
require_once (‘functions.php’);
$conn = db_connect();
?>
Update Record
<?php
if (isset($_SESSION['name']) &&
isset($_SESSION['id'])
){
$id = $_SESSION['id'];
$name = $_SESSION['name'];
$player = query_form_echo($conn, $_SESSION['id']);
?>
Name: |
|
Number |
|
Position |
|
Team |
|
|
|
<?php
}
else {
header("Location: record.php"); // Redirecting to the Records page if nothing was selected. You can change it if you want.
}
if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’ &&
!empty($_POST[‘name’]) &&
!empty($_POST[‘number’]) &&
!empty($_POST[‘position’]) &&
!empty($_POST[‘team’]) &&
isset($_POST[‘recUpdate’])
) {
$name = $_POST['name'];
$number = $_POST['number'];
$position = $_POST['position'];
$team = $_POST['team'];
query_rec_eupdate($conn, $_SESSION['id'], $name, $number,
$position, $team);
header("Location: record.php");
}
?>
<?php
mysqli_close($conn);
?>
[/php]