Page 1 of 1
How to show the database information on the site
Posted: Thu Dec 17, 2009 4:41 pm
by cat_fich
I already wrote the following code:
Code: Select all
<?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
Re: How to show the database information on the site
Posted: Thu Dec 17, 2009 10:23 pm
by josh
Not only do you want me to code it for you but you didnt tell me what it should be doing and that how the differs from what it does now? You make it tempting for us to help someone else instead.
Re: How to show the database information on the site
Posted: Thu Dec 17, 2009 10:51 pm
by califdon
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.
The very first thing you need to do is understand what that function is intended to do, which you can do by reading the online MySQL manual. mysql_last_insert_id() only returns the value of the most recently assigned auto-increment ID. Showing "all the information stored in the database" requires a SQL query and code to fetch all the rows and display the data. Instead of posting an open-ended question in a forum like this, you would benefit much more from spending a little time reviewing the basic fundamentals of PHP and MySQL.
When you have specific questions that are not obvious by reading tutorials or the manual, come back here and we will be glad to try to help you.
Re: How to show the database information on the site
Posted: Fri Dec 18, 2009 8:57 am
by cat_fich
califdon wrote: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.
The very first thing you need to do is understand what that function is intended to do, which you can do by reading the online MySQL manual. mysql_last_insert_id() only returns the value of the most recently assigned auto-increment ID. Showing "all the information stored in the database" requires a SQL query and code to fetch all the rows and display the data. Instead of posting an open-ended question in a forum like this, you would benefit much more from spending a little time reviewing the basic fundamentals of PHP and MySQL.
When you have specific questions that are not obvious by reading tutorials or the manual, come back here and we will be glad to try to help you.
My question is specific and I've read the manual.
Let me put it this way. I have the following code:
Code: Select all
<?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>
And I get 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?
Code: Select all
$results = print_r($b, true); // $results now contains output from print_r[/CODE9
But when I change this line:
Code: Select all
Campo Saida: <input type="text" name="campo_saida" value="<?php print_r($row); ?>
to:
Code: Select all
Campo Saida: <input type="text" name="campo_saida" value="<?php print_r($row, true); ?>
the form fields become empty. What am I doing wrong?
Re: How to show the database information on the site
Posted: Fri Dec 18, 2009 9:27 am
by papa
Code: Select all
echo $row['campo1']
echo $row['campo2']
Re: How to show the database information on the site
Posted: Fri Dec 18, 2009 3:01 pm
by cat_fich
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
Re: How to show the database information on the site
Posted: Fri Dec 18, 2009 3:26 pm
by cat_fich
Ok I removed the if((isset($id)).
Thanks once again.
Is it possible to implement radio buttons on the survey?
Re: How to show the database information on the site
Posted: Sat Dec 19, 2009 2:38 am
by josh
cat_fich wrote:Is it possible to implement radio buttons on the survey?
What do you think?
Re: How to show the database information on the site
Posted: Sat Dec 19, 2009 1:12 pm
by cat_fich
josh wrote:cat_fich wrote:Is it possible to implement radio buttons on the survey?
What do you think?
I know it's possible because I already did it now lol.
I was testing the form 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.
Code: Select all
<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
Re: How to show the database information on the site
Posted: Sat Dec 19, 2009 4:25 pm
by cat_fich
Problem solved.
Thanks