PDA

Click to See Complete Forum and Search --> : Display MySQL data


Olly79
Feb 8th, 2009, 04:42 AM
Hi all,

Just wondering if someone can help me with the following.

I'm inserting data from a web form into MySQL (below is the data that I'm inserting):

$Body .= $_POST['m'].' '.$name.''.$surname.'<br />';
if(!empty($_POST['month']) && !empty($_POST['day']) && !empty($_POST['year']) )$Body .= 'Date of Birth: '.$_POST['month'].' '.$_POST['day'].' '.$_POST['year'].'<br />';
if(!empty($_POST['gender']))$Body .= 'Gender: '.$_POST['gender'].'<br />';
if(!empty($address))$Body .= 'Street Address: '.$address.'<br />';
if(!empty($city))$Body .= 'City: '.$city.'<br />';
if(!empty($zip))$Body .= 'Zip Code: '.$zip.'<br />';
if(!empty($_POST['country']))$Body .= 'Country: '.$_POST['country'].'<br />';
if(!empty($state))$Body .= 'State/Province: '.$state.'<br />';
if(!empty($day_ph))$Body .= 'Daytime phone: '.$day_ph.'<br />';
if(!empty($mob_ph))$Body .= 'Mobile Telephone: '.$mob_ph.'<br />';
if(!empty($email))$Body .= 'Email: '.$email.'<br />';
if(!empty($add_info))$Body .= 'Additional Information: '.$add_info.'<br />';

I now want to display this data on a web form I have:

<form action="insert.php" method="post">
MySQL Table Name:
<div class="indent-form"><input type="text"/></div>
<br />
<textarea name="mysql recruitment data" rows="20" cols="150" wrap="physical"></textarea>
<br />
<INPUT TYPE="submit" value="load data">
<INPUT TYPE="submit" value="export data" />
</form>

I basically wanted to load the data via the "load data" button, and I would firstly load the table name into the first textbox and then all the data for the database named "recruitment" would be loaded into the textarea in tabluar format.

I have seen ways in which I can load MySQL data; however, I'm struggling with how I can achieve this based on what I want to do.

Any help would be much appreciated.

kows
Feb 8th, 2009, 07:32 AM
I think by "database" you meant table -- or at least I'd hope so. this does what you want.
<textarea><?php
$sql = mysql_query(" SELECT * FROM table");
$f = 0; //for showing the fields.
while($arr = mysql_fetch_array($sql)){
if($f == 0){
$f++; //this is so we never show the fields again
//dump fields
$rl = 0; //just to make a separator
$j = 1; //field count
foreach($arr as $key => $value){
$j++;
if($j % 2){ //mysql will give us numeric and text values as keys, we want to skip numeric
$rl += strlen($key);
echo strtoupper($key) . "\t";
}
}
echo "\n" . str_repeat("-", $rl) . "\n"; //this is the separator
}
$i = 1;
foreach($arr as $key => $value){
$i++;
if($i % 2){ //again, skipping numeric field values
echo $value . "\t";
}
}
echo "\n";
}
?></textarea>

Olly79
Feb 8th, 2009, 08:05 AM
Hi,

Yes, I meant the TABLE recruitment.

Thanks for the assistance; however, I'm going to struggle with how I can put this all together.

I wanted a 'load data' buton which then loads the TABLE into the textarea for which you have given me an example of; however, I'm not sure how I would add that to the form and then how to call it via the 'load data' button.

Could you further assist?

Many thanks

Olly79
Feb 8th, 2009, 08:13 AM
Hi,

Okay, if I was to use the following:

<textarea name="comments" rows="20" cols="150" wrap="physical"><?php
$sql = mysql_query(" SELECT * FROM table");
$f = 0; //for showing the fields.
while($arr = mysql_fetch_array($sql)){
if($f == 0){
$f++; //this is so we never show the fields again
//dump fields
$rl = 0; //just to make a separator
$j = 1; //field count
foreach($arr as $key => $value){
$j++;
if($j % 2){ //mysql will give us numeric and text values as keys, we want to skip numeric
$rl += strlen($key);
echo strtoupper($key) . "\t";
}
}
echo "\n" . str_repeat("-", $rl) . "\n"; //this is the separator
}
$i = 1;
foreach($arr as $key => $value){
$i++;
if($i % 2){ //again, skipping numeric field values
echo $value . "\t";
}
}
echo "\n";
}
?></textarea>
<br />
<INPUT TYPE="submit" value="load data">
<INPUT TYPE="submit" value="export data" />

How could I call this data in the 'load data' button effectively based on the code you have done for me?

Thanks

kows
Feb 8th, 2009, 09:20 AM
you could implement it into something like this:
<?php
$table = (isset($_POST['table']) && $_POST['table'] != "") ? $_POST['table'] : "";
?>
<form action="insert.php" method="post">
<input type="text" name="table" value="<?php echo $table; ?>" />
<textarea>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" && $table != ""){
//table code here
}
?>
</textarea>
<input type="submit" value="Load Data" />
</form>

Olly79
Feb 8th, 2009, 09:36 AM
Hi,

Sorry, but I'm now lost...my fault totally as I'm a PHP newbie!

I'm not sure how the form should now look at what code should be where?

Based on everything you have shown me and my form can you please show me how the completed page should now look and where I should be including the sample connection below:

<?php
$database="mydbname";
mysql_connect ("localhost", "myusername", "mypassword");
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query( "SELECT * FROM recruitment" )
or die("SELECT Error: ".mysql_error());

Thanks again.

kows
Feb 8th, 2009, 09:45 AM
uhh.. the database stuff goes at the beginning of the page. and the PHP script I included earlier replaces the comment that reads, "//table code here."

Olly79
Feb 8th, 2009, 10:19 AM
Hi,

Okay, would I be along the correct lines with the following?

<?php
$database="mydbname";
mysql_connect ("localhost", "myusername", "mypassword");
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query( "SELECT * FROM recruitment" )
or die("SELECT Error: ".mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
$table = (isset($_POST['table']) && $_POST['table'] != "") ? $_POST['table'] : "";
?>
<form action="insert.php" method="post">
<input type="text" name="table" value="<?php echo $table; ?>" />
<textarea>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" && $table != ""){
$sql = mysql_query(" SELECT * FROM table");
$f = 0; //for showing the fields.
while($arr = mysql_fetch_array($sql)){
if($f == 0){
$f++; //this is so we never show the fields again
//dump fields
$rl = 0; //just to make a separator
$j = 1; //field count
foreach($arr as $key => $value){
$j++;
if($j % 2){ //mysql will give us numeric and text values as keys, we want to skip numeric
$rl += strlen($key);
echo strtoupper($key) . "\t";
}
}
echo "\n" . str_repeat("-", $rl) . "\n"; //this is the separator
}
$i = 1;
foreach($arr as $key => $value){
$i++;
if($i % 2){ //again, skipping numeric field values
echo $value . "\t";
}
}
echo "\n";
}
?>
</textarea>
<input type="submit" value="Load Data" />
</form>
</body>
</html>

However, the above does not lay out the tabel as before when I had:

<textarea name="recruitment" rows="20" cols="150" wrap="physical">

Therefore, can you assit in what I would need to amend?

Thanks

Olly79
Feb 8th, 2009, 10:19 AM
p.s. I really appreciate you taking the time to help me!

kows
Feb 9th, 2009, 03:21 AM
you seriously need to consult some VERY basic tutorials on HTML and PHP. if you read through the script you pasted together, there is a <textarea> tag. you need to modify that tag and add the name, rows, cols, and wrap parameters (that you quoted in your previous post) to it. that's it.

it's one thing to ask for help, but it doesn't seem like you're trying too hard on your own.

Olly79
Feb 9th, 2009, 05:56 AM
Hi,

I would have tried; however, this had to be complete by the close of today and that didn't really afford me the time to get my head around everything. However, this is something I intend on learning once this is out of the way.

Okay, I would be very grateful if you could cast your eyes over the following code I now have:

<?php
$database="mydbname";
mysql_connect ("localhost", "myusername", "mypassword");
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query( "SELECT * FROM recruitment" )
or die("SELECT Error: ".mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>MySQL Data Load</title>
</head>

<body>
<?php
$table = (isset($_POST['table']) && $_POST['table'] != "") ? $_POST['table'] : "";
?>
<form action="insert.php" method="post">
<input type="text" name="table" value="<?php echo $table; ?>" />
<br />
<textarea name="recruitment" rows="20" cols="150" wrap="physical">
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" && $table != ""){
$sql = mysql_query(" SELECT * FROM table");
$f = 0; //for showing the fields.
while($arr = mysql_fetch_array($sql)){
if($f == 0){
$f++; //this is so we never show the fields again
//dump fields
$rl = 0; //just to make a separator
$j = 1; //field count
foreach($arr as $key => $value){
$j++;
if($j % 2){ //mysql will give us numeric and text values as keys, we want to skip numeric
$rl += strlen($key);
echo strtoupper($key) . "\t";
}
}
echo "\n" . str_repeat("-", $rl) . "\n"; //this is the separator
}
$i = 1;
foreach($arr as $key => $value){
$i++;
if($i % 2){ //again, skipping numeric field values
echo $value . "\t";
}
}
echo "\n";
}
?>
</textarea>
<br />
<input type="submit" value="Load Data" />
</form>
</body>
</html>

Does, the above look okay to you?

Thanks

kows
Feb 9th, 2009, 03:27 PM
it looks fine to me I guess? but I haven't run the script. I've only ran the part I wrote originally, without the form. you're the person running the code, you tell me if that works for you or not ;)