|
-
Feb 8th, 2009, 05:42 AM
#1
Thread Starter
Hyperactive Member
Display MySQL data
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):
PHP Code:
$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:
HTML Code:
<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.
-
Feb 8th, 2009, 08:32 AM
#2
Re: Display MySQL data
I think by "database" you meant table -- or at least I'd hope so. this does what you want.
PHP Code:
<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>
-
Feb 8th, 2009, 09:05 AM
#3
Thread Starter
Hyperactive Member
Re: Display MySQL data
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
-
Feb 8th, 2009, 09:13 AM
#4
Thread Starter
Hyperactive Member
Re: Display MySQL data
Hi,
Okay, if I was to use the following:
PHP Code:
<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
-
Feb 8th, 2009, 10:20 AM
#5
Re: Display MySQL data
you could implement it into something like this:
PHP Code:
<?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>
-
Feb 8th, 2009, 10:36 AM
#6
Thread Starter
Hyperactive Member
Re: Display MySQL data
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 Code:
<?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.
-
Feb 8th, 2009, 10:45 AM
#7
Re: Display MySQL data
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."
-
Feb 8th, 2009, 11:19 AM
#8
Thread Starter
Hyperactive Member
Re: Display MySQL data
Hi,
Okay, would I be along the correct lines with the following?
PHP Code:
<?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:
HTML Code:
<textarea name="recruitment" rows="20" cols="150" wrap="physical">
Therefore, can you assit in what I would need to amend?
Thanks
-
Feb 8th, 2009, 11:19 AM
#9
Thread Starter
Hyperactive Member
Re: Display MySQL data
p.s. I really appreciate you taking the time to help me!
-
Feb 9th, 2009, 04:21 AM
#10
Re: Display MySQL data
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.
-
Feb 9th, 2009, 06:56 AM
#11
Thread Starter
Hyperactive Member
Re: Display MySQL data
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 Code:
<?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
-
Feb 9th, 2009, 04:27 PM
#12
Re: Display MySQL data
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 ;)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|