How to show the database information on the site

Discussion in 'PHP' started by cat_fich, Dec 17, 2009.

  1. #1
    I already wrote the following code:

    <?php
       
        // CONEXÃO À BASE DE DADOS
            $dbConnection = mysql_connect("localhost", "root", "") or die("Não foi possível estabelecer uma ligação ao MYSQL!");
            $dbSelected = mysql_select_db("proj",$dbConnection) or die("Não foi possível seleccionar a Base de Dados! |Conexão ao MYSQL: ".$dbConnection);
           
        if(isset($_POST['botao_submit'])) {
            $campo1 = $_POST['campo1'];
            $campo2 = $_POST['campo2'];
           
            if(!(empty($campo1) || empty($campo2))) {
       
                   // CONSULTA
                mysql_query("INSERT INTO form(campo1,campo2) VALUES('".$_POST['campo1']."','".$_POST['campo2']."')");
                $id = mysql_insert_id();
            }
        }
       
            if(isset($id)) {
                $get = mysql_query("SELECT * FROM form WHERE id=".$id);
                $record = mysql_fetch_assoc($get);
            }          
    ?>
    
    <html>
    <head>
    <title>Formulario</title>
    </head>
    
    <body>
    <form action="form.php" method="POST">
     Campo1: <input type="text" name="campo1" />
     Campo2: <input type="text" name="campo2" />
     <input type="submit" name="botao_submit">
    </form>
    
    <?php if(isset($record)){ ?>
    <form action="form.php" method="POST">
     Campo Saida1: <input type="text" name="campo_saida1" value="<?php echo $record['campo1']; ?>" />
     Campo Saida2: <input type="text" name="campo_saida2" value="<?php echo $record['campo2']; ?>" />
    </form>
    <?php } ?>
    </body>
    </html>
    Code (markup):
    It's working but mysql_last_insert_id(); only returns the last values inserted into the campo1 and campo2 fields. I wanted to show all the information stored in the database.

    My ultimate aim is to replace the fields campo1 and campo2 by the questions of a survey and show the answers submitted by all the users in the formulary below or in another page.

    I used mysql_last_insert_id(); because like I haven't programmed in PHP for 2 years, I collected information here and there but I only realized it doesn't do all I want.

    If you can help me adapting the code I'll be very grateful
     
    cat_fich, Dec 17, 2009 IP
  2. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #2
    you can probably just query it this way to get the last record inserted

    $get = mysql_query("SELECT * FROM table ORDER BY whateverID DESC");
     
    creativeGenius, Dec 17, 2009 IP
  3. cat_fich

    cat_fich Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The code I posted is already showing the last record inserted.
    I want to change the code so it shows all the records inserted in the database and not only the last
     
    cat_fich, Dec 17, 2009 IP
  4. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #4
    $get = mysql_query("SELECT * FROM table ORDER BY whateverID DESC");
    while($row = mysql_fetch_assoc($get)
    {
    print_r($row);
    //put html or whatever formatting here
    }

    there you go ^^
    j
     
    creativeGenius, Dec 17, 2009 IP
  5. cat_fich

    cat_fich Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I changed the code:

    <?php
    
    // CONEXÃO À BASE DE DADOS
    $dbConnection = mysql_connect("localhost", "root", "") or die("Não foi possível estabelecer uma ligação ao MYSQL!");
    $dbSelected = mysql_select_db("proj",$dbConnection) or die("Não foi possível seleccionar a Base de Dados! |Conexão ao MYSQL: ".$dbConnection);
    
    if(isset($_POST['botao_submit'])) {
    $campo1 = $_POST['campo1'];
    $campo2 = $_POST['campo2'];
    
    if(!(empty($campo1) || empty($campo2))) {
    
    // CONSULTA
    mysql_query("INSERT INTO form(campo1,campo2) VALUES('".$_POST['campo1']."','".$_POST['campo2']."')");
    $id = mysql_insert_id();
    }
    }
    
    if(isset($id)) {
    $get = mysql_query("SELECT campo1, campo2 FROM form ORDER BY id ASC");
    while($row = mysql_fetch_assoc($get)) {
    //put html or whatever formatting here
    ?>
    <form action="" method="POST">
    Campo Saida: <input type="text" name="campo_saida" value="<?php print_r($row); ?>" />
    </form>
    <?php
    }
    }
    ?>
    
    <html>
    <head>
    <title>Formulario</title>
    </head>
    
    <body>
    <form action="form.php" method="POST">
    Campo1: <input type="text" name="campo1" />
    Campo2: <input type="text" name="campo2" />
    <input type="submit" name="botao_submit">
    </form>
    </body>
    </html>
    Code (markup):
    And I got this:
    [​IMG]

    In the second line, for example, I want to show a and b instead of Array ( [campo1] => a [campo2] => b )
    I've already read the manual http://php.net/manual/en/function.print-r.php and from what I understood I need to do something like this right?
    $results = print_r($b, true); // $results now contains output from print_r
    Code (text):
    But when I change this line:
    Campo Saida: <input type="text" name="campo_saida" value="<?php print_r($row); ?>
    Code (markup):
    to:
    Campo Saida: <input type="text" name="campo_saida" value="<?php print_r($row, true); ?>
    Code (markup):
    the form fields become empty. Do you know what am I doing wrong? Thanks
     
    cat_fich, Dec 18, 2009 IP
  6. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #6
    you should echo the individual array elements

    eg:
    echo $row['campo1'];
    echo $row['campo2'];

    and so on...
     
    creativeGenius, Dec 18, 2009 IP
  7. cat_fich

    cat_fich Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thank you very much for your help, now it's working like I wanted with a little exception. Is there a way to show the information to all the users? Now it only shows the information to the users who click on the sumbit button but I wanted to show the information to all the users, even those who don't click the submit button
     
    cat_fich, Dec 18, 2009 IP
  8. cat_fich

    cat_fich Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ok I removed the if((isset($id)).
    Thanks once again.
    Is it possible to implement radio buttons on the survey?
     
    Last edited: Dec 18, 2009
    cat_fich, Dec 18, 2009 IP
  9. cat_fich

    cat_fich Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    was testing the form now and I faced with this situation:

    [​IMG]

    If the database is empty it shows the text Campo Saída 1, Campo Saída 2 and Género.
    I want to show this only if the database isn't empty.

    <form action="form.php" method="POST">
     if() {
     Campo 1:
     } 
     <input type="text" name="campo1" /><br>
     Campo 2: 
     <input type="text" name="campo2" /><br>
     <input type="radio" name ="genero" value= "masculino" checked="checked">Masculino
     <input type="radio" name ="genero" value= "feminino">
     Feminino
     <br>
     <input type="submit" name="botao_submit">
     <input type="reset" name="botao_reset">
    </form>
    <br>
    Code (markup):
    I don't know what condition I've to put inside the if's
     
    cat_fich, Dec 19, 2009 IP
  10. cat_fich

    cat_fich Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Problem solved.
    Thanks
     
    cat_fich, Dec 19, 2009 IP