[SOLVED] Query Results to Forms on website
Moderator: General Moderators
[SOLVED] Query Results to Forms on website
Hello, I have a question. I have a database set up that contains information and I would like to query, let say a person's name, and put that person's name in a txtbox. what would the code look like for something like that. I have about forty fields, but if I can figure out one, I can figure out them all.
Thank you
Thank you
-
Illusionist
- Forum Regular
- Posts: 903
- Joined: Mon Jan 12, 2004 9:32 pm
btw... i think he's talking about doing something like this :
the input boxes will appear with the values in your database for the First Name in the first box, and the Last Name in the second box.
Code: Select all
<?php
$sql = "SELECT * from mytable";
$result = mysql_query($result);
while($row=mysql_fetch_array($result))
{
echo '<form method="POST" action="bob.php">';
echo '<input type="text" name="first_name" value="'.$row['fname'].'">';
echo '<input type="text" name="last_name" value="'.$row['lname'].'">';
echo '</form>';
}
?>the input boxes will appear with the values in your database for the First Name in the first box, and the Last Name in the second box.
-
legaljuicer
- Forum Newbie
- Posts: 6
- Joined: Sat Mar 06, 2004 1:32 pm
The above code helps some. How would I use the above code in the code I have below? I have a page that a user logs into the database with and then I want the users info to show up in a table with a form so he can update any fields that need to be.
Code: Select all
<?
if(!isset($PHPSESSID))
{
//echo "no PHP session started";
header('Location: login3.php');
}
else
session_start();
include 'conf.php';
echo "Welcome, ". $username .". <BR><BR> You are now in our exclusive 'Trainees Only' section.</u><br><br>";
# connect to database
$cid = mysql_connect($host, $usr, $pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
# setup SQL statement
$SQL = " SELECT * FROM clientinfo WHERE username='$username'";
# execute SQL statement
$result = mysql_db_query($db, $SQL, $cid);
# check for errors
if (!$result) { echo( mysql_error()); }
else {
printf("Name: %s<br>\n", mysql_result($result,0,"name"));
}
echo "<br><br><br><b><a href=logout.php>Logout</a><br>";
print "<br><br><br><br>";
?>
// Added PHP tags for eyecandy -- JAMJust ideas...
How to type it, may vary (the eternal heredoc, echo/printf discussions). The vital parts I'm showing is a version on how to display the data you have stored.
Be aware, that you need something unique to distinguish between the different rows in the database...
Hence I used a hidden-field that should/could contain the id or in other way unique value of the row...
Code: Select all
// cut
if (!$result) { echo( mysql_error()); }
else {
echo '
<form method="post" action="update.php">
<input type="hidden" name="id" value="'.$result['id'].'" />
<input type="text" name="fname" value="'. $result['fname'].'" />
<input type="text" name="lname" value="'. $result['lname'].'" />
<input type="submit" value="Update" />
</form>';
}
// end cutBe aware, that you need something unique to distinguish between the different rows in the database...
Hence I used a hidden-field that should/could contain the id or in other way unique value of the row...
-
legaljuicer
- Forum Newbie
- Posts: 6
- Joined: Sat Mar 06, 2004 1:32 pm
I put the code into mine but it shows the form boxes but no data. I have an id key but it does not seem to work. What am I doing wrong?
<?
if(!isset($PHPSESSID))
{
//echo "no PHP session started";
header('Location: login3.php');
}
else
session_start();
include 'conf.php';
echo "Welcome, ". $username .". <BR><BR> You are now in our exclusive 'Trainees Only' section.</u><br><br>";
# connect to database
$cid = mysql_connect($host, $usr, $pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
# setup SQL statement
$SQL = " SELECT * FROM clientinfo WHERE username='$username'";
# execute SQL statement
$result = mysql_db_query($db, $SQL, $cid);
# check for errors
if (!$result) { echo( mysql_error()); }
else {
echo'<form method="post" action="update.php">';
echo'<input type="hidden" name="id" value="'.$result['id'].'" />';
echo'<input type="text" name="username" value="'. $result['username'].'" />';
echo'<input type="text" name="name" value="'. $result['name'].'" />';
echo'<input type="submit" value="Update" />';
echo'</form>';
printf("Name: %s<br>\n", mysql_result($result,0,"name"));
}
echo "<br><br><br><b><a href=logout.php>Logout</a><br>";
print "<br><br><br><br>";
?>
<?
if(!isset($PHPSESSID))
{
//echo "no PHP session started";
header('Location: login3.php');
}
else
session_start();
include 'conf.php';
echo "Welcome, ". $username .". <BR><BR> You are now in our exclusive 'Trainees Only' section.</u><br><br>";
# connect to database
$cid = mysql_connect($host, $usr, $pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
# setup SQL statement
$SQL = " SELECT * FROM clientinfo WHERE username='$username'";
# execute SQL statement
$result = mysql_db_query($db, $SQL, $cid);
# check for errors
if (!$result) { echo( mysql_error()); }
else {
echo'<form method="post" action="update.php">';
echo'<input type="hidden" name="id" value="'.$result['id'].'" />';
echo'<input type="text" name="username" value="'. $result['username'].'" />';
echo'<input type="text" name="name" value="'. $result['name'].'" />';
echo'<input type="submit" value="Update" />';
echo'</form>';
printf("Name: %s<br>\n", mysql_result($result,0,"name"));
}
echo "<br><br><br><b><a href=logout.php>Logout</a><br>";
print "<br><br><br><br>";
?>
try this:
i think this will work for ya.
Code: Select all
<?php
$result = mysql_db_query($db, $SQL, $cid);
$row = mysql_fetch_array($result);
// now your input line may look like this:
echo'<input type="hidden" name="id" value="'.$row['id'].'" />';
?>-
legaljuicer
- Forum Newbie
- Posts: 6
- Joined: Sat Mar 06, 2004 1:32 pm
now it quit working and I haven't changed anything. The loginsucces.php doesn't even show the username. Can anyone tell me what I am doing wrong?
login3.php
========================
<?
function login_form()
{
?>
<html>
<head>
<title> IronAddicts.com - Training Log Database </title>
</head>
<body bgcolor="#ffffff" text="#336699">
<hr noshade height=1>
<form method=post>
<table border=0>
<tr>
<td>username: </td>
<td><input type=text name=http_user></td>
</tr>
<tr>
<td>password: </td>
<td><input type=password name=http_pass></td>
</tr>
<tr>
<td colspan=2><input type=submit value="login"></td>
</tr>
</table>
</form>
<hr noshade height=1>
</body>
</html>
<?
}
if ((!isset($http_user)) or (!isset($http_pass)))
{
login_form();
exit;
}
else
{
include ('conf.php');
$dbh = mysql_connect($host,$usr,$pwd) or die("Could not connect to database");
mysql_select_db ($db, $dbh) or die("Could not select the database");
$sql = "select * from clients where username='$http_user' and password='$http_pass'";
$res = mysql_query($sql);
$err = mysql_error();
$num_rows = mysql_num_rows($res);
if ($num_rows != 1)
{
login_form();
exit;
}
else
{
session_start();
session_register('username');
$username = $http_user;
header('Location: loginsuccess.php');
//echo "you are authenticated as $http_user with password $http_pass<br>";
}
mysql_close($dbh);
}
?>
loginsuccess.php
<?
if(!isset($PHPSESSID))
{
//echo "no PHP session started";
header('Location: login3.php');
}
else
session_start();
include 'conf.php';
echo "Welcome, ". $username .". <BR><BR> You are now in our exclusive 'Trainees Only' section.</u><br><br>";
# connect to database
$cid = mysql_connect($host, $usr, $pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
# setup SQL statement
$SQL = " SELECT * FROM clientinfo WHERE username='$username'";
# execute SQL statement
$result = mysql_db_query($db, $SQL, $cid);
$row = mysql_fetch_array($result);
# check for errors
if (!$result) { echo( mysql_error()); }
else {
echo'<form method="post" action="update.php">';
echo'<input type="hidden" name="id" value="'.$row['id'].'" />';
echo'<input type="text" name="username" value="'. $row['username'].'" />';
echo'<input type="text" name="name" value="'. $row['name'].'" />';
echo'<input type="submit" value="Update" />';
echo'</form>';
printf("Name: %s<br>\n", mysql_result($result,0,"name"));
}
echo "<br><br><br><b><a href=logout.php>Logout</a><br>";
print "<br><br><br><br>";
?>
login3.php
========================
<?
function login_form()
{
?>
<html>
<head>
<title> IronAddicts.com - Training Log Database </title>
</head>
<body bgcolor="#ffffff" text="#336699">
<hr noshade height=1>
<form method=post>
<table border=0>
<tr>
<td>username: </td>
<td><input type=text name=http_user></td>
</tr>
<tr>
<td>password: </td>
<td><input type=password name=http_pass></td>
</tr>
<tr>
<td colspan=2><input type=submit value="login"></td>
</tr>
</table>
</form>
<hr noshade height=1>
</body>
</html>
<?
}
if ((!isset($http_user)) or (!isset($http_pass)))
{
login_form();
exit;
}
else
{
include ('conf.php');
$dbh = mysql_connect($host,$usr,$pwd) or die("Could not connect to database");
mysql_select_db ($db, $dbh) or die("Could not select the database");
$sql = "select * from clients where username='$http_user' and password='$http_pass'";
$res = mysql_query($sql);
$err = mysql_error();
$num_rows = mysql_num_rows($res);
if ($num_rows != 1)
{
login_form();
exit;
}
else
{
session_start();
session_register('username');
$username = $http_user;
header('Location: loginsuccess.php');
//echo "you are authenticated as $http_user with password $http_pass<br>";
}
mysql_close($dbh);
}
?>
loginsuccess.php
<?
if(!isset($PHPSESSID))
{
//echo "no PHP session started";
header('Location: login3.php');
}
else
session_start();
include 'conf.php';
echo "Welcome, ". $username .". <BR><BR> You are now in our exclusive 'Trainees Only' section.</u><br><br>";
# connect to database
$cid = mysql_connect($host, $usr, $pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
# setup SQL statement
$SQL = " SELECT * FROM clientinfo WHERE username='$username'";
# execute SQL statement
$result = mysql_db_query($db, $SQL, $cid);
$row = mysql_fetch_array($result);
# check for errors
if (!$result) { echo( mysql_error()); }
else {
echo'<form method="post" action="update.php">';
echo'<input type="hidden" name="id" value="'.$row['id'].'" />';
echo'<input type="text" name="username" value="'. $row['username'].'" />';
echo'<input type="text" name="name" value="'. $row['name'].'" />';
echo'<input type="submit" value="Update" />';
echo'</form>';
printf("Name: %s<br>\n", mysql_result($result,0,"name"));
}
echo "<br><br><br><b><a href=logout.php>Logout</a><br>";
print "<br><br><br><br>";
?>