Page 1 of 1

[SOLVED] Query Results to Forms on website

Posted: Thu Mar 04, 2004 7:45 pm
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

Posted: Thu Mar 04, 2004 7:47 pm
by Illusionist
SELECT name FROM tbl_users WHERE id=$id

$id being the person's id number

Posted: Thu Mar 04, 2004 8:40 pm
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.

Posted: Thu Mar 04, 2004 11:35 pm
by infolock
***moved to databases***

Posted: Thu Mar 04, 2004 11:39 pm
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.

Posted: Sat Mar 06, 2004 1:32 pm
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

Posted: Sun Mar 07, 2004 12:31 pm
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...

Posted: Sun Mar 07, 2004 2:02 pm
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>";


?>

Posted: Sun Mar 07, 2004 2:14 pm
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.

Posted: Sun Mar 07, 2004 2:18 pm
by legaljuicer
awesome
thanks that worked

Posted: Sat Mar 20, 2004 4:05 pm
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>";


?>