I have a database with two tables:
CREATE TABLE IF NOT EXISTS `players` ( `id` int(11) NOT NULL AUTO_INCREMENT, `firstname` varchar(32) NOT NULL, `lastname` varchar(32) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=19 ;
and the second Table:
CREATE TABLE IF NOT EXISTS `teams` ( `id` int(11) NOT NULL AUTO_INCREMENT, `team_name` varchar(32) NOT NULL, `player_id` varchar(32) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=127 ;
The following form allows me to "Add a new record", "delete" or "add more info" into Table "players" database:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/*
VIEW.PHP
Displays all data from 'players' table
*/
// connect to the database
include('connect-db.php');
// get results from database
$result = mysql_query("SELECT * FROM players ORDER BY lastname")
or die(mysql_error());
// display data in table
echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Edit</th> <th>Delete</th><th>Add more</th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['firstname'] . '</td>';
echo '<td>' . $row['lastname'] . '</td>';
echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
echo '<td><a href="add.php?id=' . $row['id'] . '">Add more info</a></td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
<p><a href="new.php">Add a new record</a></p>
</body>
</html>
The "Add more info" is linked to a second form called "add.php" which in turn inserts team_name as follows:
<html>
<form action="" method="post">
<strong>Team Name: *</strong> <input type="text" name="team_name"><br/>
<input type="submit" name="submit" value="Submit">
</form>
</html>
<?php
$player_id = (int)($_GET['id']);
$team = $_POST['team_name'];
// connect to the database
include('connect-db.php');
// save the data to the database
mysql_query("INSERT INTO teams (team_name, player_id) VALUES('$team','$player_id')")
or die(mysql_error());
?>
I'm not sure if the form is any good, and this is where I run into trouble:
The form inserts (id, team_name, and player_id) "teams" table however it inserts it twice: once with the team_name in and the other with blank. And I don't know how to redirect the form to another page after submission.
Your help will is highly appreciated.
PHP is keeping late into the night every night.
Thank you all.

New Topic/Question
Reply




MultiQuote





|