Forum Moderators: coopster

Message Too Old, No Replies

Not Putting Values in Form Fields

         

xtremer360

6:15 pm on Mar 13, 2010 (gmt 0)

10+ Year Member



Its not putting the values from the query into the form fields and I don't know why?

$query = "SELECT s.hometown as hometown, s.height as height, s.weight as weight,
h.kowtitles as kowtitles, h.kowawards as kowawards,
w.nicknames as nicknames, w.finisher as finisher, w.setup as setup, w.music as music
FROM `efed_bio_singles` AS s,
`efed_bio_history` AS h,
`efed_bio_wrestling` AS w
WHERE s.bio_id = '{$defaultcharacterid}',
h.bio_id = '{$defaultcharacterid}',
w.bio_id = '{$defaultcharacterid}'";
$result = mysql_query($query);
echo $result;
?>
<h1 class=backstage>Character Management</h1><br />
<h2 class=backstage><?php echo $defaultcharactername; ?> - Personal</h2><br />
<form name="editcharacter" method="post">
<table width="100%" class="table2">
<tr>
<td width=120 class=rowheading>Hometown:</td><td class=row3>
<input type=text name=hometown class=fieldtext490 value="<?php echo $hometown; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Height:</td><td class=row3>
<input type=text name=height class=fieldtext40 value="<?php echo $row['height']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Weight:</td><td class=row3>
<input type=text name=weight class=fieldtext80 value="<?php echo $row['weight']; ?>"></td>
</tr>
</table><br />
<h2 class=backstage>KOW Related</h2><br />
<table width="100%" class="table2">
<tr>
<td width=120 class=rowheading>KOW Titles:</td><td class=row3>
<input type=text name=kowtitles class=fieldtext490 value="<?php echo $row['kowtitles']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>KOW Awards:</td><td class=row3>
<input type=text name=kowawards class=fieldtext490 value="<?php echo $row['kowawards']; ?>"></td>
</tr>
</table><br />
<h2 class=backstage>Wrestling</h2><br />
<table width="100%" class="table2">
<tr>
<td width=120 class=rowheading>Nicknames:</td><td class=row3>
<input type=text name=nicknames class=fieldtext490 value="<?php echo $row['nicknames']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Alignment:</td><td class=row3>
<select name=alignmentid class=dropdown>
<option value=1>-Select-</option>
<?php
$query = 'SELECT * FROM efed_list_alignment';
$result = mysql_query ( $query );
while ( $alignment_row = mysql_fetch_assoc ( $result ) )
{
print "<option value=\"".$alignment_row['id']."\" ";
if($alignment_row['id'] == $row['alignment_id']) {
print " SELECTED";
}
print ">".$alignment_row['name']."</option>\r";
}
?>
</select></td>
</tr>
<tr>
<td width=120 class=rowheading>Manager:</td><td class=row3>
<select name=managerid class=dropdown>
<option value=0>- Select -</option>
<?php
$query = 'SELECT charactername,id FROM `efed_bio` WHERE `style_id`= 3';
$result = mysql_query ( $query );
while ( $manager_row = mysql_fetch_assoc ( $result ) )
{
print "<option value=\"".$manager_row['id']."\" ";
if($manager_row['id'] == $row['manager_id']) {
print " SELECTED";
}
print ">".$manager_row['charactername']."</option>\r";
}
?>
</select></td>
</tr>
<tr>
<td width=120 class=rowheading>Finisher Move:</td><td class=row3>
<input type=text name=finisher_name class=fieldtext490 value="<?php echo $row['finisher']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Setup Move:</td><td class=row3>
<input type=text name=setup_name class=fieldtext490 value="<?php echo $row['setup']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Entrance Music:</td><td class=row3>
<input type=text name=entrancemusic class=fieldtext490 value="<?php echo $row['music']; ?>"></td>
</tr>
</table><br />
<input type=submit value="Update Bio" class=button></form><br />
<?php
returnmain();

Readie

6:29 pm on Mar 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have not declared the $row array, or the $hometown variable

xtremer360

8:06 pm on Mar 13, 2010 (gmt 0)

10+ Year Member



Okay I added one in but it's still giving me that Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource error message.

$query = "SELECT s.hometown as hometown, s.height as height, s.weight as weight,
h.kowtitles as kowtitles, h.kowawards as kowawards,
w.nicknames as nicknames, w.finisher as finisher, w.setup as setup, w.music as music
FROM `efed_bio_singles` AS s,
`efed_bio_history` AS h,
`efed_bio_wrestling` AS w
WHERE s.bio_id = '{$defaultcharacterid}',
h.bio_id = '{$defaultcharacterid}',
w.bio_id = '{$defaultcharacterid}'";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$fieldarray=array('hometown','height','weight','kowtitles','kowawards','nicknames','finisher','setup','arena','music');
foreach ($fieldarray as $fieldlabel)
{
if (isset($row[$fieldlabel]))
{
$$fieldlabel=$row[$fieldlabel];
$$fieldlabel=cleanquerydata($$fieldlabel);
}
}
}
?>
<h1 class=backstage>Character Management</h1><br />
<h2 class=backstage><?php echo $defaultcharactername; ?> - Personal</h2><br />
<form name="editcharacter" method="post">
<table width="100%" class="table2">
<tr>
<td width=120 class=rowheading>Hometown:</td><td class=row3>
<input type=text name=hometown class=fieldtext490 value="<?php echo $row['hometown']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Height:</td><td class=row3>
<input type=text name=height class=fieldtext40 value="<?php echo $row['height']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Weight:</td><td class=row3>
<input type=text name=weight class=fieldtext80 value="<?php echo $row['weight']; ?>"></td>
</tr>
</table><br />
<h2 class=backstage>KOW Related</h2><br />
<table width="100%" class="table2">
<tr>
<td width=120 class=rowheading>KOW Titles:</td><td class=row3>
<input type=text name=kowtitles class=fieldtext490 value="<?php echo $row['kowtitles']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>KOW Awards:</td><td class=row3>
<input type=text name=kowawards class=fieldtext490 value="<?php echo $row['kowawards']; ?>"></td>
</tr>
</table><br />
<h2 class=backstage>Wrestling</h2><br />
<table width="100%" class="table2">
<tr>
<td width=120 class=rowheading>Nicknames:</td><td class=row3>
<input type=text name=nicknames class=fieldtext490 value="<?php echo $row['nicknames']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Alignment:</td><td class=row3>
<select name=alignmentid class=dropdown>
<option value=1>-Select-</option>
<?php
$query = 'SELECT * FROM efed_list_alignment';
$result = mysql_query ( $query );
while ( $alignment_row = mysql_fetch_assoc ( $result ) )
{
print "<option value=\"".$alignment_row['id']."\" ";
if($alignment_row['id'] == $row['alignment_id']) {
print " SELECTED";
}
print ">".$alignment_row['name']."</option>\r";
}
?>
</select></td>
</tr>
<tr>
<td width=120 class=rowheading>Manager:</td><td class=row3>
<select name=managerid class=dropdown>
<option value=0>- Select -</option>
<?php
$query = 'SELECT charactername,id FROM `efed_bio` WHERE `style_id`= 3';
$result = mysql_query ( $query );
while ( $manager_row = mysql_fetch_assoc ( $result ) )
{
print "<option value=\"".$manager_row['id']."\" ";
if($manager_row['id'] == $row['manager_id']) {
print " SELECTED";
}
print ">".$manager_row['charactername']."</option>\r";
}
?>
</select></td>
</tr>
<tr>
<td width=120 class=rowheading>Finisher Move:</td><td class=row3>
<input type=text name=finisher_name class=fieldtext490 value="<?php echo $row['finisher']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Setup Move:</td><td class=row3>
<input type=text name=setup_name class=fieldtext490 value="<?php echo $row['setup']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Entrance Music:</td><td class=row3>
<input type=text name=entrancemusic class=fieldtext490 value="<?php echo $row['music']; ?>"></td>
</tr>
</table><br />
<input type=submit value="Update Bio" class=button></form><br />
<?php
returnmain();

rocknbil

8:52 pm on Mar 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First,

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result


can mean you have an error in your mysql syntax. Do this,

$result = mysql_query($query) or error(mysql_error());

and that will become obvious.

But let's back up a step. You're making it harder than it has to be. :-) Watch.

while ($row = mysql_fetch_assoc($result)){

$row is already an associative array of values. If you did an r_print of $row here, you would get

Array [
field1 => value1,
field2 => value2,
etc. . . .

So there's no need for "fieldarray."

$output=NULL;
if ($row = mysql_fetch_assoc($result)){

$output .= '<tr><td><input type="text" value="' . $row['hometown'] . '"></td></tr>';
}

if ($output) {
echo "
<table> $output </table>
";
}
else { echo " no results "; }

I see the reasoning, you're trying to create variables of variables, eventually you will succeed, but this just complicates your work.

Two important differences here.

The first is when you pull a single record, use if. True, a while does the same thing, but if you get in this habit, one day you will wonder why it's pulling the last record in your database when it should have pulled an earlier one. Using if when appropriate alerts you much earlier to a problem in your select logic.

The second thing is the intermittent mix of html and PHP echo's is very common, but IMO makes it very difficult to maintain and debug. In extremely large pages, you will eventually encounter a page that half-renders, waits, half renders, waits . . . in the same way we separate presentation from content with CSS, it's a good idea to separate programming loops and functions from strict HTML output, rather than string them together. It will make your job much easier.

Note above how it compiles the output in programming, echo's/outputs only once, and outputs an error message if there's no results. This is not possible, at least, gracefully, using the PHP/HTML mixed methods.

If you do all of the above and get " no results" turn your attention here:

echo "$query";

This will reveal all. I could be wrong, but am wondering what the function of the curlies are here?

'{$defaultcharacterid}',


table test
id|name|f1|f2
123|rocknbil|blah|whatever

$defaultcharacterid = 'rocknbil';

select * from table where name='{$defaultcharacterid}'

gives you

select * from test where name='{rocknbil}'

Which obviously will return zero rows. That is, unless the curlies have some mySQL function I've never seen (entirely possible.)

xtremer360

9:58 pm on Mar 13, 2010 (gmt 0)

10+ Year Member



I took out that field array and took out the as parts for the table fields since I wouldn't need that anymore I'm sure. If you can see I have echoed the query and it echoes correctly with the right defaultcharacterid but will not post the corresponding field data from my tables and their fields.

$query = "SELECT s.hometown, s.height, s.weight,
h.kowtitles, h.kowawards,
w.nicknames, w.finisher, w.setup, w.music
FROM `efed_bio_singles` AS s,
`efed_bio_history` AS h,
`efed_bio_wrestling` AS w
WHERE s.bio_id = '$defaultcharacterid',
h.bio_id = '$defaultcharacterid',
w.bio_id = '$defaultcharacterid'";
$result = mysql_query($query);
echo $query;
?>
<h1 class=backstage>Character Management</h1><br />
<h2 class=backstage><?php echo $defaultcharactername; ?> - Personal</h2><br />
<form name="editcharacter" method="post">
<table width="100%" class="table2">
<tr>
<td width=120 class=rowheading>Hometown:</td><td class=row3>
<input type=text name=hometown class=fieldtext490 value="<?php echo $row['hometown']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Height:</td><td class=row3>
<input type=text name=height class=fieldtext40 value="<?php echo $row['height']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Weight:</td><td class=row3>
<input type=text name=weight class=fieldtext80 value="<?php echo $row['weight']; ?>"></td>
</tr>
</table><br />
<h2 class=backstage>KOW Related</h2><br />
<table width="100%" class="table2">
<tr>
<td width=120 class=rowheading>KOW Titles:</td><td class=row3>
<input type=text name=kowtitles class=fieldtext490 value="<?php echo $row['kowtitles']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>KOW Awards:</td><td class=row3>
<input type=text name=kowawards class=fieldtext490 value="<?php echo $row['kowawards']; ?>"></td>
</tr>
</table><br />
<h2 class=backstage>Wrestling</h2><br />
<table width="100%" class="table2">
<tr>
<td width=120 class=rowheading>Nicknames:</td><td class=row3>
<input type=text name=nicknames class=fieldtext490 value="<?php echo $row['nicknames']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Manager:</td><td class=row3>
<select name=managerid class=dropdown>
<option value=0>- Select -</option>
<?php
$query = 'SELECT charactername,id FROM `efed_bio` WHERE `style_id`= 3';
$result = mysql_query ( $query );
while ( $manager_row = mysql_fetch_assoc ( $result ) )
{
print "<option value=\"".$manager_row['id']."\" ";
if($manager_row['id'] == $row['manager_id']) {
print " SELECTED";
}
print ">".$manager_row['charactername']."</option>\r";
}
?>
</select></td>
</tr>
<tr>
<td width=120 class=rowheading>Finisher Move:</td><td class=row3>
<input type=text name=finisher class=fieldtext490 value="<?php echo $row['finisher']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Setup Move:</td><td class=row3>
<input type=text name=setup class=fieldtext490 value="<?php echo $row['setup']; ?>"></td>
</tr>
<tr>
<td width=120 class=rowheading>Entrance Music:</td><td class=row3>
<input type=text name=music class=fieldtext490 value="<?php echo $row['music']; ?>"></td>
</tr>
</table><br />
<input type="hidden" name="editted2" value="true">
<input type=submit value="Update Bio" class=button></form><br />

Matthew1980

7:07 pm on Mar 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there xtremer360,

Just wondering, what are you using to action this script? :-

<form name="editcharacter" method="post" action="myfile.php">

If there is no action set, nothing will happen, sounds stupid, but have you echoed the $_POST array after the button is pressed to see what is going on, firstly though, unless I have read the script wrong, you need to set the action, this can either be by $_SERVER['PHP_SELF'] (refreshes the page) or by pointing to: 'update.php' type of file...


Cheers,
MRb

Readie

9:03 pm on Mar 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If there is no action set, nothing will happen

Not true, I often do not set a form action on my forms and am still able to retrieve my data through $_POST. All it means is you need to process it in the same file.

rocknbil

9:51 pm on Mar 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



(Edit: referring to M's post) If it's all the same script, a missing action value will post to itself. This is one of the ways to "work around" the vulnerabilities of $_SERVER['PHP_SELF'] if you're ever compelled to use that.

I see two things happening here. One is easy, one may be more difficult.

First the easy:

$result = mysql_query($query);
?>

Where's the rest of it? :-) You have the resource in $result, but need to fetch it.

$result = mysql_query($query);
if ($row=mysql_fetch_array($result)) {
echo $row['hometown'];
}
?>

Do that first before anything else . . . make sure it's pulling data.

You didn't add the error handler, so we've no way of knowing if it's a valid resource. If you get "invalid resource" after adding the fetch, you have a problem with the select.

Something I'd missed before - and makes me think uh-huh, this is going to error - review your mySQL where syntax.

select [fields] from [tables] where [condition1 and|or condition2....]

Look at your where, you have commas after where:

WHERE s.bio_id = '$defaultcharacterid',
h.bio_id = '$defaultcharacterid',
w.bio_id = '$defaultcharacterid'";

The second problem is you are querying multiple tables and matching ID on the input value - this won't work. It will return rows, but will return a row for table 1, a row for table 2 . . . and if there are multiple rows (like, user's images) a row for each of those.

You need to JOIN the supplemental tables on the first table by some foreign key. This may not be right (but then, it might), but it's closer:

$query = "SELECT s.hometown, s.height, s.weight,
h.kowtitles, h.kowawards,
w.nicknames, w.finisher, w.setup, w.music
FROM `efed_bio_singles` AS s,
`efed_bio_history` AS h,
`efed_bio_wrestling` AS w
WHERE s.bio_id = '$defaultcharacterid' and h.bio_id = s.bio_id and w.bio_id = s.bio_id";

See how the relational tables are all joining on the first table, not the value itself?

Other options are using the more pure inner, outer, etc. joins . . . but a direct join like this works too.

Matthew1980

9:56 pm on Mar 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Readie [Edit] & Rocknbil,

Well I guess that I am not in the habit of leaving it undefined then. Just tried an example and checked out the w3c reference lib, and much to my surprise it defaults to itself if nothing is defined, so its just the same as having $_SERVER['PHP_SELF']; specified. I only ever use $_SERVER['PHP_SELF']; when testing out a theories on functions I have not used before ;-p

Cheers,
MRb

[edited by: Matthew1980 at 10:00 pm (utc) on Mar 14, 2010]

rocknbil

9:59 pm on Mar 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



so its just the same as having $_SERVER['PHP_SELF'];


It acts the same, but it's not the same. :-) G for "PHP_SELF vulnerability". :-)

I agree though . . . I never leave them empty.

Matthew1980

10:13 pm on Mar 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Rocknbil,

Thanks for the pointer :-p, I had no idea, thankfully nothing I set into the public domain uses this, and a good job too from what I have read up to yet.

Cheers,
MRb