Coders2018 Posted July 7, 2010 Share Posted July 7, 2010 Hello, I'm fairly new to PHP & MySQL and I bought a book which has so far guided me perfectly to where I want to be at this stage. I did however decide to take a rest from the book and see what I had learned so far by building what I've always wanted to a PHP & MySQL Football League just like the Premier League one. I've set up my database and a few tables which I think are correct but I'm not entirely sure. I've created an HTML form which will send the information to the .PHP file and that will then add the team selected to the TeamName's field in my database table. This all works perfectly. Here is the code for that: <html> <head> <title>League Results</title> </head> <body> <center> <form action="add_teams.php" method="post"> <?php // Database Connection Information $username = "MY_USERNAME"; $password = "MY_USER_PASSWORD"; $database = "MY_DATABASE_NAME"; // Variables passed from the HTML Form $team_name = $_POST['Team_Name']; // Connect to the database mysql_connect(localhost, $username, $password); @mysql_select_db($database) or die ("Unable to connect to the database provided"); // Insert Team into database $addteam = mysql_query("INSERT INTO Teams (TeamName) VALUES ('$_POST[Team_Name]')"); // Display on the screen confirmation of the team that's been added. if($addteam == True) { echo "<center>The team you requested $team_name have been added to the database"; } else { echo "<center>The team $team_name could not be added to the database</center>"; } mysql_close(); ?> <input type="submit" name="Add_Another_Team" value="Add Another Team"> </form> </body> </html> The problem I'm now having is that I don't know how to display those teams that are selected on another PHP page. I'm using MySQL Server version: 5.1.44. Please help as I'm truly stuck. Quote Link to comment https://forums.phpfreaks.com/topic/207028-displaying-what-ive-entered/ Share on other sites More sharing options...
Coders2018 Posted July 7, 2010 Author Share Posted July 7, 2010 I've managed to come up with some code but that only displays the field names and not the data inside them. I would like it so that the data is under the correct field name. Here is the code I've managed to come up with on my "view_teams.php" page. <html> <head> <title> View Teams </title> </head> <body> <?php // Make an MySQL Connection $username = "USERNAME"; $password = "PASSWORD"; $database = "DATABASE"; mysql_connect(localhost, $username, $password); @mysql_select_db($database) or die ("Unable to connect to the database provided"); // Create a query to select the data from the table and display it. $query = mysql_query("SELECT * FROM `Teams`"); $teamname = mysql_query($query); echo mysql_error(); echo mysql_field_name($query, 0) . "\n"; echo mysql_field_name($query, 1); // Use a while loop to go through how much data there is. while($teams = mysql_fetch_array($teamname)) { echo "$teams[TeamID] $teams[TeamName]<br>"; } ?> </body> </html> Please can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/207028-displaying-what-ive-entered/#findComment-1082766 Share on other sites More sharing options...
Coders2018 Posted July 8, 2010 Author Share Posted July 8, 2010 Surely someone on here must know how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/207028-displaying-what-ive-entered/#findComment-1082905 Share on other sites More sharing options...
Mchl Posted July 8, 2010 Share Posted July 8, 2010 The code seems ok. Are you sure there is data in Teams table? Quote Link to comment https://forums.phpfreaks.com/topic/207028-displaying-what-ive-entered/#findComment-1082927 Share on other sites More sharing options...
Coders2018 Posted July 8, 2010 Author Share Posted July 8, 2010 The code seems ok. Are you sure there is data in Teams table? Thanks for replying. There is data in the Teams table and somehow I've managed to get the field names and the data inside them to display on the screen. The problem I'm having now is that the data is not going under the correct field names. For example the TeamName field is directly at the side of the TeamID field and I want them to be spread apart. Also the data which goes in both these fields is displaying at the side of the field names and not under them. Here is the new code I managed to put together: <html> <head> <title> View Teams </title> </head> <body> <?php // Make an MySQL Connection $username = "USERNAME"; $password = "PASSWORD"; $database = "DATABASE_NAME"; mysql_connect(localhost, $username, $password); @mysql_select_db($database) or die ("Unable to connect to the database provided"); // Create a query to select the data from the table and display it. $query = mysql_query("SELECT * FROM `Teams`"); echo mysql_error(); $rowcount = mysql_num_rows($query); $y = mysql_num_fields($query); for ($x = 0; $x < $y; $x++) { echo mysql_field_name($query, $x); } // Use a while loop to go through how much data there is. while($teams = mysql_fetch_array($query)) { echo "$teams[TeamID] $teams[TeamName]<br>"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/207028-displaying-what-ive-entered/#findComment-1082961 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.