Jump to content

Update of data using PHP truncates text data


jej1216

Recommended Posts

I have a series of scripts that allow the user to a) create new data to insert into a MySQL DB, b) review that data, and c) change that data and do an update to the MySQL DB.

 

For a), I use a basic html form, and it passes a sample value like this:

Severity of Incident:
<select name="severity" size="1">
<option value="">Select a Severity Option</option>
<option value="Level1 - No Obvious Harm">Level 1 - No Obvious Harm</option>
<option value="Level2 - Non-permanent Harm">Level 2 - Non-permanent Harm</option>
<option value="Level3 - Semi-permanent Harm">Level 3 - Semi-permanent Harm</option>
<option value="Level4 - Major Permanent Harm">Level 4 - Major Permanent Harm</option>
<option value="Level5 - Death">Level 5 - Death</option>
</select>

 

This inserts the full text into the DB, and it shows this full text for b).

 

For c), I use a php page, and it displays the value correctly using this:

 

$sev=$myrow["severity"];

 

and

 

<?php echo "<option value = ".$sev.">".$sev."</option>"; ?>

followed by the choices if needing to be changed:

<option value="Level1 - No Obvious Harm">Level 1 - No Obvious Harm</option>
<option value="Level2 - Non-permanent Harm">Level 2 - Non-permanent Harm</option>
<option value="Level3 - Semi-permanent Harm">Level 3 - Semi-permanent Harm</option>
<option value="Level4 - Major Permanent Harm">Level 4 - Major Permanent Harm</option>
<option value="Level5 - Death">Level 5 - Death</option>
</select>

If I re-select the same choice in the php page, the data is again submitted to the DB and the update is the full text value. If, however, I leave that data alone and submit, it gets truncated to 'Level2.'

 

The PHP page is not passing the

<?php echo "<option value = ".$sev.">".$sev."</option>"; ?>

value.

 

This issue occurs only for the drop down fields. I have other text fields that are not drop-downs and they do not truncate.

 

I have searched other posts and it seems I need to enclose $sev with quotes, but I get errors when I try that.

 

TIA,

 

jej1216

 

as a start, maybe try

 

<?php echo "<option value = '".htmlspecialchars($sev,ENT_QUOTES)."'>".$sev."</option>"; ?>

 

edit: you should avoid using the entire string as the option value. i would use an integer and store the string values in either a database or an array. for instance:

 

<?php
$severities = array('Select a Severity Option', 'Level 1 - No Obvious Harm', 'Level 2 - Non-permanent Harm','Level 3 - Semi-permanent Harm','Level 4 - Major Permanent Harm','Level 5 - Death');
?>
<select name="severity" size="1">
<?php
$counter = 0;
foreach($severities AS $a_severity) {
    echo "<option value = '$counter'>".$severities[$counter]."</option>";
    $counter++;
}
?>
</select>

 

fully untested.

BlueSkyIS -- thanks for the response.

 

I tried the first code snippet, and got the same issue.

 

I tried the second code, but have these two issues:

 

1) I want to keep the selected value in case the user does not need to change that value.  With this second code snippet, I lose that selected value (the $sev value).

 

2) When I use the second snippet and select for example "Level 2 - Non-permanent Harm" and submit the form, the data base shows "2" as the value.  I guess it's passing the counter value?  If the user opens the data again, I want them to see "Level 2 - Non-permanent Harm" instead of "2."  And, if the user does not want to change the data, how will that existing data be included in the counter (or is that not possible?)?

 

The main issue for me is that I want the user to pull up the existing data, and either change or not change it and resubmit.  If the user picks new data, it inserts correctly into the DB.  It's only when the user does not change the value (and only a drop-down text value at that) that the data inserted gets truncated at the first space.

 

Thanks,

jej1216

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.