Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

How to clean form input for SQL (apostrophes, symbols)

  • 01-02-2012 06:23PM
    #1
    Banned (with Prison Access) Posts: 1,940 ✭✭✭


    I have a form coded in HTML, which uses PHP to insert the data to a MySQL table.

    Apostrophes, currency symbols and I presume slashes all screw it up.

    How can I fix this?

    Bear in mind that I don't simply want to strip these characters, as I will need them for output.


Comments

  • Registered Users, Registered Users 2 Posts: 2,062 ✭✭✭Colonel Panic


    Use mysql_real_escape_string to escape potentially evil stuff without stripping out anything.


  • Banned (with Prison Access) Posts: 1,940 ✭✭✭BhoscaCapall


    Thanks, that sorts the apostrophes out, however I still get grief with the currency symbols

    I tried "€10 on the door / £8 before" as a test and this is what appeared in my table:
    €8 10n the door / £8 before

    Do I need to manually code it to replace £ with £ etc or is there an inbuilt function?


  • Registered Users, Registered Users 2 Posts: 1,757 ✭✭✭Deliverance XXV


    Thanks, that sorts the apostrophes out, however I still get grief with the currency symbols

    I tried "€10 on the door / £8 before" as a test and this is what appeared in my table:


    Do I need to manually code it to replace £ with £ etc or is there an inbuilt function?

    This is something I wondered about myself until I outputted it on a webpage and it outputted perfectly anyway.


  • Closed Accounts Posts: 8,015 ✭✭✭CreepingDeath


    The standard approach for cleaning SQL entry is to use prepared statements.
    This will also protect you from SQL injection attacks which expose your website to hackers.

    PHP Prepared Statement


  • Closed Accounts Posts: 249 ✭✭OneIdea


    Thanks, that sorts the apostrophes out, however I still get grief with the currency symbols

    I tried "€10 on the door / £8 before" as a test and this is what appeared in my table:


    Do I need to manually code it to replace £ with £ etc or is there an inbuilt function?

    You could try this for now:
    http://www.php.net/manual/en/function.htmlentities.php
    [PHP]
    mysql_real_escape_string(htmlentities($inputData));
    [/PHP]

    It will convert, example currency symbols in to html codes before they enter your database, and thus they will display correctly when printed out to the page.


  • Advertisement
Advertisement