" mess up my INSERT into query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Swedie
    New Member
    • May 2001
    • 26

    " mess up my INSERT into query

    Hey

    I'm doing a little chat script and when I submit a post that includes the character ' it totally mess up the Query script.

    In what way I can bypass that problem? I need to be able to use odd characters like that.

    Thanks in advance.
  • GameCrash
    Senior Member
    • Oct 2000
    • 422
    • 3.6.x

    #2
    Try this:

    PHP Code:
    $message addslashes($message); 
    GameCrash

    Project Tools Importer for forum threads
    Current Version: 1.0.0 Beta 3

    Comment

    • Icheb
      Senior Member
      • Nov 2002
      • 1291

      #3
      addslashes(htmlspecialchars($variable)) would be safer actually.

      Comment

      • Chen
        Senior Member
        • Jun 2001
        • 8388

        #4
        Actually you shouldn't htmlspecialchars() it when placing in the database, only in runtime when the data is displayed. That way you don't lose its original state...
        Chen Avinadav
        Better to remain silent and be thought a fool than to speak out and remove all doubt.

        גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

        Comment

        • Swedie
          New Member
          • May 2001
          • 26

          #5
          Originally posted by Chen
          Actually you shouldn't htmlspecialchars() it when placing in the database, only in runtime when the data is displayed. That way you don't lose its original state...
          Ok, so how do I submit something into the database that includes ' ... ?

          the post is through a <form> and goes to a new page, post.php. Where a query inserts it to the database like this:

          mysql_query("INSERT INTO message VALUES ('', $message, '$bbuserinfo[username]', '$bbuserinfo[userid]', '$posttime', '$category', '$color');") or die("MySQL Query Failed: " . mysql_error());
          I'd like help.

          Comment

          • XTJ7
            New Member
            • Jul 2002
            • 15

            #6
            actually i prefer something like
            Code:
            mysql_query( "INSERT INTO `messages` VALUES ( '', '" . addslashes( $message ) . "', '" . addslashes( $bbuserinfo[ 'username' ] ) . "', '" . addslashes( $posttime ) . "', '" . addslashes( $category ) . "', '" . addslashes( $color ) . "' );" ) or die("MySQL Query Failed: " . mysql_error() );
            is much safer, but i recommend to give mysql the fields with the table so that you just insert the fields you had been selected.
            like
            Code:
            INSERT INTO `messages` (`name`, `info`, `others`) VALUES ( 'xtj7', 'programmer', 'foo' );
            so you can leave out id and it increments automatically without giving mysql an empty field.

            c:ya

            Comment

            • Swedie
              New Member
              • May 2001
              • 26

              #7
              what's wrong in this query?

              mysql_query("INSERT INTO message ('message', 'postername', 'posterid', 'category', 'color') VALUES ( '" . addslashes( $message ) . "', '" . addslashes( $bbuserinfo[username] ) . "', '" . addslashes( $bbuserinfo[userid] ) . "', '" . addslashes( $category ) . "', '" . addslashes( $color ) . "');") or die("MySQL Query Failed: " . mysql_error());


              I get:
              MySQL Query Failed: You have an error in your SQL syntax near ''message', 'postername', 'posterid', 'category', 'color') VALUES ( 'sadsda', 'Wh' at line 1

              Comment

              • Chen
                Senior Member
                • Jun 2001
                • 8388

                #8
                Field names should be quoted with single quotes - either use backstick (`) or don't quote them at all.
                Chen Avinadav
                Better to remain silent and be thought a fool than to speak out and remove all doubt.

                גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

                Comment

                • Swedie
                  New Member
                  • May 2001
                  • 26

                  #9
                  figured it out...

                  chen

                  what is a backstick.. never heard about it and .. hence not knowing why that should be used.. ever.. care to let me know?

                  Comment

                  • Chen
                    Senior Member
                    • Jun 2001
                    • 8388

                    #10
                    It's just the name of this character: `
                    Chen Avinadav
                    Better to remain silent and be thought a fool than to speak out and remove all doubt.

                    גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

                    Comment

                    • Swedie
                      New Member
                      • May 2001
                      • 26

                      #11
                      but when is that backstick supposed to be used?

                      Comment

                      • Icheb
                        Senior Member
                        • Nov 2002
                        • 1291

                        #12
                        You use

                        INSERT INTO message ('message', 'postername', 'posterid', 'category', 'color')

                        You should use

                        INSERT INTO message (`message`, `postername`, `posterid`, `category`, `color`)

                        Or just don't use them at all.

                        Comment

                        • GameCrash
                          Senior Member
                          • Oct 2000
                          • 422
                          • 3.6.x

                          #13
                          It's allways used if you use a reserved keyword as a table or column name for example...
                          GameCrash

                          Project Tools Importer for forum threads
                          Current Version: 1.0.0 Beta 3

                          Comment

                          widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                          Working...