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
you can probably just query it this way to get the last record inserted $get = mysql_query("SELECT * FROM table ORDER BY whateverID DESC");
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
$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
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: 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
you should echo the individual array elements eg: echo $row['campo1']; echo $row['campo2']; and so on...
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
Ok I removed the if((isset($id)). Thanks once again. Is it possible to implement radio buttons on the survey?
was testing the form now and I faced with this situation: 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