use and retain a field value if already set

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phphlp47
Forum Newbie
Posts: 11
Joined: Mon Mar 08, 2010 6:49 pm

use and retain a field value if already set

Post by phphlp47 »

I have a MYSQL DB with 4 fields that may or may not be set each time a logged in user accesses the input form.
What I would like to happen is if the field has a value then display it on the HTML/PHP form with the option to change it if required.
If a new value is not put into the input form box then the value originally there needs to be used/retained.
I have tried the following;
if (isset($row_settings['Height']))
{
$Height = $row_settings['Height'];
}

but this like the many other coding options tried before simply lose the original field value.
The FORM value name is Height - in this example.
Should I be using different FORM value name to the DB field name?
if so how do I code it.
Sorry I have googled this in many ways but do not seem to be able to sort it.

Could someone please advise the correct way to code this to achieve the desired result.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: use and retain a field value if already set

Post by JakeJ »

If isset() works out to be true $Height SHOULD by all means retain that value. Of course if it return false, $Height will not be set at all.

Have you tested to see $Height is actually getting the value you think it is? Echo out $row_settings['Height'] to see what the value is. Also make sure you haven't set a global variable to be $Height.

You didn't give the rest of your code, but make sure you're not trying to use $Height outside of loop unless you declare it to be global or it won't show up.

Your approach is fine, you're setting $Height so that the variable is retained. If it's not there when it should be, it means you need to look elsewhere for the problem.
phphlp47
Forum Newbie
Posts: 11
Joined: Mon Mar 08, 2010 6:49 pm

Re: use and retain a field value if already set

Post by phphlp47 »

JakeJ wrote:If isset() works out to be true $Height SHOULD by all means retain that value. Of course if it return false, $Height will not be set at all.

Have you tested to see $Height is actually getting the value you think it is? Echo out $row_settings['Height'] to see what the value is. Also make sure you haven't set a global variable to be $Height.

You didn't give the rest of your code, but make sure you're not trying to use $Height outside of loop unless you declare it to be global or it won't show up.

Your approach is fine, you're setting $Height so that the variable is retained. If it's not there when it should be, it means you need to look elsewhere for the problem.
Tried the code below, but still the same result,
Top of the PHP code has the following to update the DB;

SQL code;

mysql_query("UPDATE users SET
`Height` = '$data[Height]',
`Weight` = '$data[Weight]',
…. (other fields)
`Last_App_Time` = '$data[Last_App_Time]',
`Next_App_Time` = '$data[Next_App_Time]'
WHERE id='$_SESSION[user_id]'
") or die(mysql_error());

HTML/PHP FORM code;
<? while ($row_settings = mysql_fetch_array($rs_settings)) {?>
….
….
<table border="1" width=90%>
<tr>
<td><span class="style20b">Height:</span></td>
<td>
<span class="style20b"><? echo $row_settings['Height']; ?></span>
<input name="Height" type="text" size="8" maxlength="8">
<?
if (isset($row_settings['Height']))
{
$Height = $row_settings['Height'];
}
else
{
$data[Height] = 'Else';
}
?>
</td>
</tr>
….
….
}
<? ?>

SAVE button at bottom of form;
<td><input name="doSave" type="submit" id="doSave" value="Save"></td>

The ECHO displays the current setting of Height OK.
Changed fields are updated OK, but,
As soon as any other field is changed, the Height field is blanked out.

Is the variable $Height the same as the FORM input box “input name=’Height’….”
Maybe I’m checking the wrong fields, i.e. I should be checking input name=”Height” rather than the existing setting using $row_settings[‘Height’]!

Many thanks for your assistance on this one.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: use and retain a field value if already set

Post by AbraCadaver »

If your table contains a field called `Height` and you SELECT * from that table and then fetch a row from that result set into to $row_settings, then $row_settings['Height'] will always be SET. But it may be empty. Try:

Code: Select all

if( !empty($row_settings['Height']) )
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
phphlp47
Forum Newbie
Posts: 11
Joined: Mon Mar 08, 2010 6:49 pm

Re: use and retain a field value if already set

Post by phphlp47 »

AbraCadaver wrote:If your table contains a field called `Height` and you SELECT * from that table and then fetch a row from that result set into to $row_settings, then $row_settings['Height'] will always be SET. But it may be empty. Try:

Code: Select all

if( !empty($row_settings['Height']) )
By using both the $row_settings and myphpadmin I can see that field is loaded with a value. my problem happens when Height is set with a value it gets wiped after any other field is changed/loaded and Height is not touched in anyway, this results in the field being wiped out with a null(blank entry) this can be confirmed by running the form again and/or checking myphpadmin DB table/field.

I have considered changing the form variable name field then if its loaded with a new value plug that into 'Height' to get it loaded/updated into the DB, but not sure of the coding required to achieve that.
phphlp47
Forum Newbie
Posts: 11
Joined: Mon Mar 08, 2010 6:49 pm

Re: use and retain a field value if already set

Post by phphlp47 »

Cured this by doing the following in the form;

<td><span class="style20b">Height:</span></td>
<td><input name="Height" type="text" value="<? echo $row_settings['Height']; ?>"></td>

This way if the input field is changed then the DB field gets updated, if its not changed then it simply retains its value.

I'm now trying to re-assign values from one field (date/time field) to another if they are updated! Will post a new request for this if I cannot get the coding right.

Many thanks for the replies, actually used the "if (!empty" on something else and it works fine.
Post Reply