Jump to content

How to show the database information on the site


cat_fich

Recommended Posts

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>

 

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

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

I was testing the form now and I faced with this situation:

 

51569418.jpg

 

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>

 

I don't know what condition I've to put inside the if's

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.