Results 1 to 12 of 12

Thread: Display MySQL data

  1. #1

    Thread Starter
    Hyperactive Member Olly79's Avatar
    Join Date
    May 2005
    Posts
    264

    Display MySQL data

    Hi all,

    Just wondering if someone can help me with the following.

    I'm inserting data from a web form into MySQL (below is the data that I'm inserting):

    PHP Code:
    $Body .= $_POST['m'].' '.$name.''.$surname.'<br />';
                if(!empty(
    $_POST['month']) && !empty($_POST['day']) && !empty($_POST['year'])  )$Body .= 'Date of Birth: '.$_POST['month'].' '.$_POST['day'].' '.$_POST['year'].'<br />';
                if(!empty(
    $_POST['gender']))$Body .= 'Gender: '.$_POST['gender'].'<br />';
                if(!empty(
    $address))$Body .= 'Street Address: '.$address.'<br />';
                if(!empty(
    $city))$Body .= 'City: '.$city.'<br />';
                if(!empty(
    $zip))$Body .= 'Zip Code: '.$zip.'<br />';
                if(!empty(
    $_POST['country']))$Body .= 'Country: '.$_POST['country'].'<br />';
                if(!empty(
    $state))$Body .= 'State/Province: '.$state.'<br />';
                if(!empty(
    $day_ph))$Body .= 'Daytime phone: '.$day_ph.'<br />';
                if(!empty(
    $mob_ph))$Body .= 'Mobile Telephone: '.$mob_ph.'<br />';
                if(!empty(
    $email))$Body .= 'Email: '.$email.'<br />';
                if(!empty(
    $add_info))$Body .= 'Additional Information: '.$add_info.'<br />'
    I now want to display this data on a web form I have:

    HTML Code:
    <form action="insert.php" method="post">
    MySQL Table Name:
    <div class="indent-form"><input type="text"/></div>
    <br />
    <textarea name="mysql recruitment data" rows="20" cols="150" wrap="physical"></textarea>
    <br />
    <INPUT TYPE="submit" value="load data">
    <INPUT TYPE="submit" value="export data" />
    </form>
    I basically wanted to load the data via the "load data" button, and I would firstly load the table name into the first textbox and then all the data for the database named "recruitment" would be loaded into the textarea in tabluar format.

    I have seen ways in which I can load MySQL data; however, I'm struggling with how I can achieve this based on what I want to do.

    Any help would be much appreciated.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Display MySQL data

    I think by "database" you meant table -- or at least I'd hope so. this does what you want.
    PHP Code:
    <textarea><?php
      $sql 
    mysql_query(" SELECT * FROM table");
      
    $f 0//for showing the fields.
      
    while($arr mysql_fetch_array($sql)){
        if(
    $f == 0){
          
    $f++; //this is so we never show the fields again
          //dump fields
          
    $rl 0//just to make a separator
          
    $j 1//field count
          
    foreach($arr as $key => $value){
            
    $j++;
            if(
    $j 2){ //mysql will give us numeric and text values as keys, we want to skip numeric
              
    $rl += strlen($key);
              echo 
    strtoupper($key) . "\t";
            }
          }
          echo 
    "\n" str_repeat("-"$rl) . "\n"//this is the separator
        
    }
        
    $i 1;
        foreach(
    $arr as $key => $value){
          
    $i++;
          if(
    $i 2){ //again, skipping numeric field values
            
    echo $value "\t";
          }
        }
        echo 
    "\n";
      }
    ?></textarea>

  3. #3

    Thread Starter
    Hyperactive Member Olly79's Avatar
    Join Date
    May 2005
    Posts
    264

    Re: Display MySQL data

    Hi,

    Yes, I meant the TABLE recruitment.

    Thanks for the assistance; however, I'm going to struggle with how I can put this all together.

    I wanted a 'load data' buton which then loads the TABLE into the textarea for which you have given me an example of; however, I'm not sure how I would add that to the form and then how to call it via the 'load data' button.

    Could you further assist?

    Many thanks

  4. #4

    Thread Starter
    Hyperactive Member Olly79's Avatar
    Join Date
    May 2005
    Posts
    264

    Re: Display MySQL data

    Hi,

    Okay, if I was to use the following:

    PHP Code:
    <textarea name="comments" rows="20" cols="150" wrap="physical"><?php
      $sql 
    mysql_query(" SELECT * FROM table");
      
    $f 0//for showing the fields.
      
    while($arr mysql_fetch_array($sql)){
        if(
    $f == 0){
          
    $f++; //this is so we never show the fields again
          //dump fields
          
    $rl 0//just to make a separator
          
    $j 1//field count
          
    foreach($arr as $key => $value){
            
    $j++;
            if(
    $j 2){ //mysql will give us numeric and text values as keys, we want to skip numeric
              
    $rl += strlen($key);
              echo 
    strtoupper($key) . "\t";
            }
          }
          echo 
    "\n" str_repeat("-"$rl) . "\n"//this is the separator
        
    }
        
    $i 1;
        foreach(
    $arr as $key => $value){
          
    $i++;
          if(
    $i 2){ //again, skipping numeric field values
            
    echo $value "\t";
          }
        }
        echo 
    "\n";
      }
    ?></textarea>
    <br />
    <INPUT TYPE="submit" value="load data">
    <INPUT TYPE="submit" value="export data" />
    How could I call this data in the 'load data' button effectively based on the code you have done for me?

    Thanks

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Display MySQL data

    you could implement it into something like this:
    PHP Code:
    <?php
      $table 
    = (isset($_POST['table']) && $_POST['table'] != "") ? $_POST['table'] : "";
    ?>
    <form action="insert.php" method="post">
      <input type="text" name="table" value="<?php echo $table?>" />
      <textarea>
    <?php
      
    if($_SERVER['REQUEST_METHOD'] == "POST" && $table != ""){
        
    //table code here
      
    }
    ?>
      </textarea>
      <input type="submit" value="Load Data" />
    </form>

  6. #6

    Thread Starter
    Hyperactive Member Olly79's Avatar
    Join Date
    May 2005
    Posts
    264

    Re: Display MySQL data

    Hi,

    Sorry, but I'm now lost...my fault totally as I'm a PHP newbie!

    I'm not sure how the form should now look at what code should be where?

    Based on everything you have shown me and my form can you please show me how the completed page should now look and where I should be including the sample connection below:

    PHP Code:
    <?php 
    $database
    ="mydbname"
    mysql_connect ("localhost""myusername""mypassword"); 
    @
    mysql_select_db($database) or die( "Unable to select database"); 
    $result mysql_query"SELECT * FROM recruitment" 
    or die(
    "SELECT Error: ".mysql_error());
    Thanks again.

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Display MySQL data

    uhh.. the database stuff goes at the beginning of the page. and the PHP script I included earlier replaces the comment that reads, "//table code here."

  8. #8

    Thread Starter
    Hyperactive Member Olly79's Avatar
    Join Date
    May 2005
    Posts
    264

    Re: Display MySQL data

    Hi,

    Okay, would I be along the correct lines with the following?

    PHP Code:
    <?php
    $database
    ="mydbname";
    mysql_connect ("localhost""myusername""mypassword");
    @
    mysql_select_db($database) or die( "Unable to select database");
    $result mysql_query"SELECT * FROM recruitment" )
    or die(
    "SELECT Error: ".mysql_error()); 
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
      $table 
    = (isset($_POST['table']) && $_POST['table'] != "") ? $_POST['table'] : "";
    ?>
    <form action="insert.php" method="post">
      <input type="text" name="table" value="<?php echo $table?>" />
      <textarea>
    <?php
      
    if($_SERVER['REQUEST_METHOD'] == "POST" && $table != ""){
          
    $sql mysql_query(" SELECT * FROM table");
      
    $f 0//for showing the fields.
      
    while($arr mysql_fetch_array($sql)){
        if(
    $f == 0){
          
    $f++; //this is so we never show the fields again
          //dump fields
          
    $rl 0//just to make a separator
          
    $j 1//field count
          
    foreach($arr as $key => $value){
            
    $j++;
            if(
    $j 2){ //mysql will give us numeric and text values as keys, we want to skip numeric
              
    $rl += strlen($key);
              echo 
    strtoupper($key) . "\t";
            }
          }
          echo 
    "\n" str_repeat("-"$rl) . "\n"//this is the separator
        
    }
        
    $i 1;
        foreach(
    $arr as $key => $value){
          
    $i++;
          if(
    $i 2){ //again, skipping numeric field values
            
    echo $value "\t";
          }
        }
        echo 
    "\n"
      }
    ?>
      </textarea>
      <input type="submit" value="Load Data" />
    </form>
    </body>
    </html>
    However, the above does not lay out the tabel as before when I had:

    HTML Code:
    <textarea name="recruitment" rows="20" cols="150" wrap="physical">
    Therefore, can you assit in what I would need to amend?

    Thanks

  9. #9

    Thread Starter
    Hyperactive Member Olly79's Avatar
    Join Date
    May 2005
    Posts
    264

    Re: Display MySQL data

    p.s. I really appreciate you taking the time to help me!

  10. #10
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Display MySQL data

    you seriously need to consult some VERY basic tutorials on HTML and PHP. if you read through the script you pasted together, there is a <textarea> tag. you need to modify that tag and add the name, rows, cols, and wrap parameters (that you quoted in your previous post) to it. that's it.

    it's one thing to ask for help, but it doesn't seem like you're trying too hard on your own.

  11. #11

    Thread Starter
    Hyperactive Member Olly79's Avatar
    Join Date
    May 2005
    Posts
    264

    Re: Display MySQL data

    Hi,

    I would have tried; however, this had to be complete by the close of today and that didn't really afford me the time to get my head around everything. However, this is something I intend on learning once this is out of the way.

    Okay, I would be very grateful if you could cast your eyes over the following code I now have:

    PHP Code:
    <?php
    $database
    ="mydbname";
    mysql_connect ("localhost""myusername""mypassword");
    @
    mysql_select_db($database) or die( "Unable to select database");
    $result mysql_query"SELECT * FROM recruitment" )
    or die(
    "SELECT Error: ".mysql_error());
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>MySQL Data Load</title>
    </head>

    <body>
    <?php
      $table 
    = (isset($_POST['table']) && $_POST['table'] != "") ? $_POST['table'] : "";
    ?>
    <form action="insert.php" method="post">
      <input type="text" name="table" value="<?php echo $table?>" />
      <br />
      <textarea name="recruitment" rows="20" cols="150" wrap="physical">
    <?php
      
    if($_SERVER['REQUEST_METHOD'] == "POST" && $table != ""){
          
    $sql mysql_query(" SELECT * FROM table");
      
    $f 0//for showing the fields.
      
    while($arr mysql_fetch_array($sql)){
        if(
    $f == 0){
          
    $f++; //this is so we never show the fields again
          //dump fields
          
    $rl 0//just to make a separator
          
    $j 1//field count
          
    foreach($arr as $key => $value){
            
    $j++;
            if(
    $j 2){ //mysql will give us numeric and text values as keys, we want to skip numeric
              
    $rl += strlen($key);
              echo 
    strtoupper($key) . "\t";
            }
          }
          echo 
    "\n" str_repeat("-"$rl) . "\n"//this is the separator
        
    }
        
    $i 1;
        foreach(
    $arr as $key => $value){
          
    $i++;
          if(
    $i 2){ //again, skipping numeric field values
            
    echo $value "\t";
          }
        }
        echo 
    "\n";
      }
    ?>
      </textarea>
      <br />
      <input type="submit" value="Load Data" />
     </form>
    </body>
    </html>
    Does, the above look okay to you?

    Thanks

  12. #12
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Display MySQL data

    it looks fine to me I guess? but I haven't run the script. I've only ran the part I wrote originally, without the form. you're the person running the code, you tell me if that works for you or not ;)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width