Current location: Hot Scripts Forums » Programming Languages » PHP » Update of data using PHP truncates text data


Update of data using PHP truncates text data

Reply
  #1 (permalink)  
Old 09-05-08, 04:42 PM
jej1216 jej1216 is offline
Newbie Coder
 
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Update of data using PHP truncates text data

I have a series of HTML and PHP pages for an Incident Reporting system. The page I have to enter initial data works, and data is inserted into a MySQL DB. The data is mostly text, many with spaces.

The PHP page that I use to select the data from a previously entered Incident report also works, displaying the data including text with spaces.

Here is the issue: The PHP page that I wrote to update the DB truncates all text data that has a space that is a dropdown field. Text that is in a text field is not affected.

Here is some of my code:

The html page that creates the report has this code (for an example) and it creates the data correctly.

HTML Code:
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>
The edit php code does this:
php displays the data from:

PHP Code:

$sev=$myrow["severity"]; 

and passes this:

HTML Code:
<select name="severity" size="1">
PHP Code:

<?php echo "<option value = ".$sev.">".$sev."</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>
and it truncates at first space in the DB "Level2".

BUT if you re-select it, then it goes into the DB without truncating. "Level2 - Non-permananet Harm"

It truncates the $sev data, if that makes sense.

I think the crux of the issue is that I am saying to update all fields, and when no change is made, the drop-down text data gets truncated.

I have tried wrapping $sev with single or double quotes but that just causes a PHP error. Is it a case where I have to somehow have it compare field by field and only update the changed fields?

TIA,

jej1216
Reply With Quote
  #2 (permalink)  
Old 09-05-08, 07:25 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
The value="..." parameter needs some double-quotes around the value.

PHP Code:

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

I also eliminated the use of concatenation as that usually results in more problems with syntax, not less.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #3 (permalink)  
Old 09-08-08, 09:17 AM
jej1216 jej1216 is offline
Newbie Coder
 
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks - but still truncates

mab,

Thanks - I tried the code you provided, but I still get the truncated field.

The $sev value shows correctly when I select it from the DB, but when I open the php to edit fields and then submit, it truncates.

This is taxing my over-taxed brain.

Any other ideas? Should I post my entire PHP code?

Thanks,

jej1216
Reply With Quote
  #4 (permalink)  
Old 09-08-08, 10:07 AM
jej1216 jej1216 is offline
Newbie Coder
 
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Problem is my update back to the DB

As I looked over my code, it dawned on me that my update to the database blows in every field, even if it has not been changed. Every single text field value that was in a drop-down type field is not only truncated, but when you open the PHP to edit, the selected field adds another choice to the drop down for the edit.

Using the Severity value $sev as an example:

When the Form is filled out, the user selects "Level 2 - Non-permanent Harm." On Submit, that value is inserted to the severity field of the DB.

When I call the value back, I make the value pulled = $sev.

When I open the PHP to edit, before I submit, the drop-down for the Severity field now has "Level 2 - Non-permanent Harm" as an additional value - appearing at the top of the list and then again in the list of 5 levels to choose from.

I think I need to code this so only changed fields get updated, but I'm not sure how to do this -- only update the field if $sev <> '$_REQUEST[severity]' ????-- do I do this in the PHP that submits the values or in the PHP that runs the Update Statement? Can anyone give me an example of how to create a dynamic Update Statement? $sql = "UPDATE jos_incidents_combined SET severity = '$_REQUEST[severity]' if $sev <> '$_REQUEST[severity]' ??????????

This table has 51 fields, so will I have to that to each and every field?

This is one of my first PHP coding tasks, certainly more complicated than the previous tasks I have had to do.

TIA,

jej1216
Reply With Quote
  #5 (permalink)  
Old 09-08-08, 12:12 PM
lindasnephew lindasnephew is offline
Newbie Coder
 
Join Date: Jan 2008
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
I think there are 2 issues here,

I think the first is in the way you are storing the data, have you had a look in the data base using phpmyadmin or something? is it truncated in the database? What data type have you chosen for your severity field?

The 2nd problem i think you are having is that you are trying to load an edit page so that you have the drop down box with the 5 levels in and the one that is saved in the database is selected.

What you are doing is creating a drop down box with the 1st option as what is pulled from your db and the next 5 are 1-5.

I personally wouldn't store the whole string "Level2 - Non-permanent harm" in your db everytime and would instead just store the number 2, this will be way better for your db in terms of size. I would recommend something like this

PHP Code:

echo '<select name="severity" id="severity">
    <option value="1"'
; echo ($sev == '1') ? ' selected="selected"' ''; echo '>Level 1</option>
    <option value="2"'
; echo ($sev == '2') ? ' selected="selected"' ''; echo '>Level 2</option>
    <option value="3"'
; echo ($sev == '3') ? ' selected="selected"' ''; echo '>Level 3</option>
    <option value="4"'
; echo ($sev == '4') ? ' selected="selected"' ''; echo '>Level 4</option>
    <option value="5"'
; echo ($sev == '5') ? ' selected="selected"' ''; echo '>Level 5</option>    
</select>'

This will save it as just the number and also will work out that if $sev = x then it should be selected.

hope that helps a little
Reply With Quote
  #6 (permalink)  
Old 09-08-08, 12:19 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
It is not exactly clear from your statement at what point the problem occurs, so it would be necessary to see all the code to provide further help. And could you indicate where in the code you believe the data is being output correctly and at what point it is not correct.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #7 (permalink)  
Old 09-08-08, 01:21 PM
jej1216 jej1216 is offline
Newbie Coder
 
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
the data inserted is not truncated in the db

The insert of data to the database does not truncate the data.

The select from the DB does not truncate the data.

It is only when the user opens the data via my PHP page that the update to the db truncates only the data value that was previously selected from the db - and only for text data that is a drop-down style. Simple text files (where the data is a typed sentence) do not get truncated on Update.

This form PHP actually inserts/updates to a 51-field table and emulates a hard-copy form used for hospital incident reporting - so the database has more user-friendly values when they do a select.

Looking at your code sample, do you mean not to include
PHP Code:

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

?

I need the user to see the previously entered value so they can determine if a change is needed.

thanks,

jej1216
Reply With Quote
  #8 (permalink)  
Old 09-09-08, 02:59 AM
lindasnephew lindasnephew is offline
Newbie Coder
 
Join Date: Jan 2008
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Yes, i don't think you should be including that line of code. i understand that you need to show the user the previously selected option but that is not really the right way of doing it as what thats doing is adding a 6th option to your drop down which is the one that they have selected.

When doing a select or drop down in html you can choose the selected option by adding selected="selected".

So in my code i have added something like this to each line

PHP Code:

echo ($sev == '1') ? ' selected="selected"' ''
That is like a really short if statement which says if $sev = 1 then add the text selected="selected" to else add '' (nothing)

By adding that to each of your select options it will find add selected="selected" to the users previously selected option without adding a 6th option to your drop down.

i still think you should consider using just numbers to store in your db then translate those numbers on the way in and out.
Reply With Quote
  #9 (permalink)  
Old 09-10-08, 08:52 AM
jej1216 jej1216 is offline
Newbie Coder
 
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks - I guess I do have to re-do my DB design.

I tried adding your code:
PHP Code:

<select name="severity" size="1">

<?php echo ($sev == '1') ? ' selected="selected"' ''?>
<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>
And it still truncates the value on submit.

If I use numeric values in the DB, then I have to code some If statements on the presentation page to show the user the verbage they need to see, right?

I appreciate your help - I'm learning a lot about PHP by putting this incident reporting system in place!


jej1216
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
text box with scroll bar silvermane CSS 7 01-16-09 03:03 AM
Submit button data written to a .txt file ziul Everything Java 0 08-08-07 03:41 PM
Search script improvement 9999 PHP 14 08-28-06 11:46 PM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 07:19 PM


All times are GMT -5. The time now is 07:54 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.