[SOLVED] Query Results to Forms on website

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
brookside
Forum Commoner
Posts: 30
Joined: Tue Mar 02, 2004 8:15 pm

[SOLVED] Query Results to Forms on website

Post by brookside »

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
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

SELECT name FROM tbl_users WHERE id=$id

$id being the person's id number
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

id = the part in your table.. people set it as the "primary key" and autocrement it so it adds a different number to each new row of data.

very handy

edit/ by different number I mean it'll add one from the previous id # to each new row, cause i know someone will correct me.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

***moved to databases***
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

btw... i think he's talking about doing something like this :

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

Post by legaljuicer »

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 -- JAM
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Just ideas...

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 cut
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...
legaljuicer
Forum Newbie
Posts: 6
Joined: Sat Mar 06, 2004 1:32 pm

Post by legaljuicer »

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>";


?>
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

try this:

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'].'" />'; 

?>
i think this will work for ya.
legaljuicer
Forum Newbie
Posts: 6
Joined: Sat Mar 06, 2004 1:32 pm

Post by legaljuicer »

awesome
thanks that worked
legaljuicer
Forum Newbie
Posts: 6
Joined: Sat Mar 06, 2004 1:32 pm

Post by legaljuicer »

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>";


?>
Post Reply