Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Fredrik Esseen 610 posts 906 karma points
    Apr 07, 2010 @ 09:41
    Fredrik Esseen
    0

    Creating a "safe" query

    Hi!

    I've created my own table in the DB and uses it to store form data. I've discovered some issues sometimes when the application crasches because of data entered in the form.

    My SQL looks like this:

    string

     

    sqlstr = "INSERT INTO EVENTREGISTEREDCUSTOMERS (Projektwebb, XML, Date) VALUES ('" + "FFF" + "', '" + xmlOutput + "', '" + DateTime.Now + "')";

    umbraco.BusinessLogic.

    Application.SqlHelper.ExecuteNonQuery(sqlstr);

    xmlOutput is a string with xml that holds the form values.

    I noticed that if a user has entered a ' the query crashes. I solved this by removing all of them before the query.

    Is there a better way to write the query to avoid this happening? Are the more "letters" to avoid?

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Apr 07, 2010 @ 09:59
    Dirk De Grave
    0

    froad,

    you should be using parametrized queries instead of plain old sql statements which are not hack safe...

    Application.SqlHelper.ExecuteNonQuery("insert into eventregisteredcustomers(projectweb, xml, date) values (@projectweb, @xml, @date)", 
      Application.SqlHelper.CreateParameter("@projectweb", "FFF"), 
      Application.SqlHelper.CreateParameter("@xml", xml), 
      Application.SqlHelper.CreateParameter("@date", DateTime.Now));

    (please do a syntax check, it's written on top of my head)

    Hope this helps.

    Regards,

    /Dirk

  • Fredrik Esseen 610 posts 906 karma points
    Apr 07, 2010 @ 11:10
    Fredrik Esseen
    0

    Thx!

    But does this automatically solve the problem with letters like ' in the form?

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Apr 07, 2010 @ 11:33
    Dirk De Grave
    0

    Yup, it should, that's what CreateParameter is for, it will unescape any special char such as ' (quote) char

     

    Cheers,

    /Dirk

  • Fredrik Esseen 610 posts 906 karma points
    Apr 07, 2010 @ 15:33
    Fredrik Esseen
    0

    Is that MySQL compatible?

    Think that is specific for MSSQL..?

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Apr 07, 2010 @ 15:59
    Dirk De Grave
    0

    Should be if you've configured to use MySql as Application.SqlHelper property will be the MySql implementation of the ISqlHelper interface.

    (not an expert on MySql...)

     

    Cheers,

    /Dirk

     

  • Fredrik Esseen 610 posts 906 karma points
    Apr 09, 2010 @ 11:44
    Fredrik Esseen
    0

    Thx!

    Got it working :)

Please Sign in or register to post replies

Write your reply to:

Draft