cat_fich Posted December 17, 2009 Share Posted December 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/185530-how-to-show-the-database-information-on-the-site/ Share on other sites More sharing options...
trq Posted December 17, 2009 Share Posted December 17, 2009 I wanted to show all the information stored in the database. Then remove the WHERE clause all together. Quote Link to comment https://forums.phpfreaks.com/topic/185530-how-to-show-the-database-information-on-the-site/#findComment-979523 Share on other sites More sharing options...
cat_fich Posted December 17, 2009 Author Share Posted December 17, 2009 I wanted to show all the information stored in the database. Then remove the WHERE clause all together. Now it only shows the first record instead of the last one lol Quote Link to comment https://forums.phpfreaks.com/topic/185530-how-to-show-the-database-information-on-the-site/#findComment-979537 Share on other sites More sharing options...
PFMaBiSmAd Posted December 17, 2009 Share Posted December 17, 2009 Your code needs a WHILE(){} loop to iterate over all the rows in the result set. Quote Link to comment https://forums.phpfreaks.com/topic/185530-how-to-show-the-database-information-on-the-site/#findComment-979538 Share on other sites More sharing options...
cat_fich Posted December 17, 2009 Author Share Posted December 17, 2009 Your code needs a WHILE(){} loop to iterate over all the rows in the result set. Can you give me an example? The number of rows is variable, what is the condition that I have to put inside the cycle? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/185530-how-to-show-the-database-information-on-the-site/#findComment-979542 Share on other sites More sharing options...
lAZLf Posted December 17, 2009 Share Posted December 17, 2009 while($row=mysql_fetch_array($get)){ echo' your data is:'.$get['datafieldhere']; } Quote Link to comment https://forums.phpfreaks.com/topic/185530-how-to-show-the-database-information-on-the-site/#findComment-979545 Share on other sites More sharing options...
cat_fich Posted December 18, 2009 Author Share Posted December 18, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/185530-how-to-show-the-database-information-on-the-site/#findComment-980265 Share on other sites More sharing options...
cat_fich Posted December 18, 2009 Author Share Posted December 18, 2009 Ok I removed the if((isset($id)). Thanks once again Quote Link to comment https://forums.phpfreaks.com/topic/185530-how-to-show-the-database-information-on-the-site/#findComment-980269 Share on other sites More sharing options...
cat_fich Posted December 18, 2009 Author Share Posted December 18, 2009 Is it possible to implement radio buttons on the survey? Quote Link to comment https://forums.phpfreaks.com/topic/185530-how-to-show-the-database-information-on-the-site/#findComment-980280 Share on other sites More sharing options...
cat_fich Posted December 19, 2009 Author Share Posted December 19, 2009 I 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> I don't know what condition I've to put inside the if's Quote Link to comment https://forums.phpfreaks.com/topic/185530-how-to-show-the-database-information-on-the-site/#findComment-980588 Share on other sites More sharing options...
cat_fich Posted December 19, 2009 Author Share Posted December 19, 2009 Problem solved. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/185530-how-to-show-the-database-information-on-the-site/#findComment-980640 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.