Jump to content

Displaying What I've Entered?


Coders2018

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.