Forum Moderators: coopster

Message Too Old, No Replies

Selected Option from Database Info?

Help

         

McBlack

6:29 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Is there a PHP code that selects information from MySQL information? Say that there's a row called firstname, and the information for row1 is Bob. The options would have been Bob, Jim, and Joe. How do I have it so someone can edit their information and the current values are preselected?

Like this.

----Name------
-----Jim------
-----Bob------ (SELECTED)
-----Joe------

but it would vary for the different users. Sorry if this is confusing.

PHP_Chimp

7:01 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The answer to your question all depends on how you have your site set up.

$sql = "SELECT * FROM some_table WHERE user='$user'";

Should (once you have changed the table and user names to suit your site) get the information you want out of your database.

If you have a list of users then you will need to get the value of $user from that list. So when you GET or POST the form you will need to use the value your user provides.
You may want to use print_r [uk2.php.net] to find out what the value of the request with user is.

You may also need to post some of your code so that we can see how you are trying to fit this into your site.

Welcome to WebmasterWorld.

McBlack

9:35 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



OK, here's my code. (of course, I'm using it with real database info)

You may want me to explain a bit more, because this is for a game. Laff Points is the health and gags are weapons.

(session.php contains information for if the user is logged in or logged out and the username info)

I'll replace the link to "#" when I figure it out. I want it so that when it's being edited, it will have already selected the current values for gags and laff points. It automatically configures the owner, so that won't need to change.

<?php
include("include/session.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<style type="text/css">
a:link {text-decoration: none; color: blue;}
a:visited {text-decoration: none; color: blue;}
a:active {text-decoration: none; color: blue;}
a:hover {text-decoration: underline; color: blue;}

body {font-family : Verdana, Geneva, Arial, Helvetica, sans-serif; background-image: url(images/bg.png);}

table.style {
border-width: 1px;
border-spacing: 2px;
border-style: solid;
border-color: gray;
border-collapse: separate;
background-color: white;
}

</style>
<title>Dust Bunny Guild - Adding A Toon</title>
</head>
<body>
<div align="center"><table width='65%' class="style"><tr><td>
<h1>Editing Toon</h1>
<br>
Your Toons:
<table align="left" border="1" cellspacing="0" cellpadding="3"><tr><td><b>Owner:</b></td><td><b>Laff Points:</b></td><td><b>Gags:</b></td><td><b>Edit:</b></td></tr>
<br>
<?
$usr = $session->username;
$username="user";
$password="pass";
$database="dbname";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM toons WHERE owner = '$usr'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$owner = mysql_result($result,$i,"owner");
$laff = mysql_result($result,$i,"laff");
$toonup = mysql_result($result,$i,"toonup");
$trap = mysql_result($result,$i,"trap");
$lure = mysql_result($result,$i,"lure");
$sound = mysql_result($result,$i,"sound");
$throw = mysql_result($result,$i,"throw");
$squirt = mysql_result($result,$i,"squirt");
$drop = mysql_result($result,$i,"drop");

echo "<tr><td>$owner</td><td>$laff</td><td>
<img src=\"images/gags/tu/$toonup.gif\">&nbsp;
<img src=\"images/gags/tr/$trap.gif\">&nbsp;
<img src=\"images/gags/lu/$lure.gif\">&nbsp;
<img src=\"images/gags/so/$sound.gif\">&nbsp;
<img src=\"images/gags/pi/$throw.gif\">&nbsp;
<img src=\"images/gags/sq/$squirt.gif\">&nbsp;
<img src=\"images/gags/dr/$drop.gif\"></td><td><a href=\"#\"><div align=\"center\">X</div></a></tr>";

$i++;
}
?>

</td></tr></table></div>

</body>
</html>

[edited by: McBlack at 9:41 pm (utc) on Jan. 21, 2008]

PHP_Chimp

7:31 pm on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you not use mysql_fetch_array [uk2.php.net] to get your result set, as opposed to looping through each of the rows to find what you want.

As the mysql_result only gets a single cell, so you are looping through to get results that may be easier to work with as an array.

What sort of a list are you after? A checkbox list so people can select there requirements, or something different.

Also could you run the query with fetching an array and just output the array (cut some of the information out if the array is very large, just leave the bits you are interested in), so we can see what is stored in each of those fields, as you will need to write some conditional statements into your code to mark the checkboxes selected and for that we need to know the values.