Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stripping two characters from the end of a string 1

Status
Not open for further replies.

Steve-vfp9user

Programmer
Feb 5, 2013
335
GB
I'm fairly new to this guys but getting there.

The below code inserts some data into a table part of which is a date which when viewed looks like: 06/06/2013--

If I reduce the amount of characters in phpmyadmin I get 06/06/20--

What I would like to do is remove the -- before the information is inserted into the table.

My code looks like this:

Code:
$sql = "INSERT INTO ".$TABLES["MYREVIEWS"]."
SET mydate_id='".$isLogged."',
[b]date_review='".$_REQUEST["date_review"]."',
[/b]reviewer='".$_REQUEST["reviewer"]."',
prod_itemitem='".$_REQUEST["prod_item"]."',
descrip='".$_REQUEST["descrip"]."'";

I have found the command RTRIM at [link ][/url] but I'm not sure of the concept of this coding.

Can someone please advise where I would add this code to mine so the -- are removed?

Many thanks

Steve
 
first off, make sure that the table column is defined as a DATE field and not as a varchar. then the database will not truncate the input.

second, for mysql and other conformed databases, you have to provide the data in a standardised format. the easiest is yyyy-mm-dd as it is conformed across international boundaries. you can do it mm-dd-yyyy but that makes no sense at all for British people as we all know that the day comes before the month ...

but on to your question:

I find it very hard to imagine how you come to receive data in a variable that looks like 'dd/mm/yyyy--'. something is going wrong in the routines used to generate the data. and I also do not see how phpmysqladmin could be causing the 3rd and fourth to last numbers to be deleted. again. something odd is going on.

so it would be best to start earlier in the process and for you to share the html form (if that is what is used) and the php code that leads you to this point, so we can establish where the abnormality occurs.

if you really do have such horribly unclean data that the input looks like dd/mm/yyyy-- then the trailing dashes can be got rid of in any of the following ways (and there are many others too)
Code:
$string = '06/06/2013--';
$string = rtrim($string, '-');

//or
$string = substr($string, 0 ,-2);
 
And it goes without saying that you should never put anything into a database without escaping it first.
 
Hi jpadie

Thank you for the very quick response. As a newbie here I appreciate the guidance.

I have changed the table field to DATE and this has fixed the problem without the need for any additional coding.

Star for you!


Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top