1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

special characters help in feedback form

Discussion in 'PHP' started by ianhaney, Apr 14, 2013.

  1. #1
    Hi

    On one of my customers websites, I have a feedback form that automatically adds the feedback to the testimonials page by adding it to the testimonials database table and displays it on the testimonials page

    I have noticed that if a special character like a ' or something, the feedback is not added

    I can't remember how to do it so that the feedback is added if it has a special character in the text

    Can someone help please

    Kind regards

    Ian
     
    ianhaney, Apr 14, 2013 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    It's most likely because the string is not being escaped, you need to check what the sql query is doing and make sure that you escape it properly.

    Post your code here and I will have a look for you.
     
    HuggyStudios, Apr 14, 2013 IP
  3. ianhaney

    ianhaney Greenhorn

    Messages:
    72
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #3
    Hi HuggyStudios


    Thank you for the reply

    I have posted the coding below from the php page that the form uses to insert the feedback, is this what you needed

    <?php
    //=============Configuring Server and Database=======
    $host        =    '';
    $user        =    '';
    $password    =    '';
    //=============Data Base Information=================
    $database    =    '';
     
    $conn        =    mysql_connect($host,$user,$password) or die('Server Information is not Correct'); //Establish Connection with Server
    mysql_select_db($database,$conn) or die('Database Information is not correct');
     
    //===============End Server Configuration============
     
    //=============Starting Registration Script==========
     
    $name    =    $_POST['name'];
     
    $testimonial    =    $_POST['testimonial'];
     
    if(isset($_POST['btnRegister'])) //===When I will Set the Button to 1 or Press Button to register
    {
    $query    =    "insert into testimonials(name,testimonial)values('$name','$testimonial')";
    $res    =    mysql_query($query);
    header('location:feedbackconfirmation.php');
    }
     
    ?>
    PHP:
     
    ianhaney, Apr 14, 2013 IP
  4. ianhaney

    ianhaney Greenhorn

    Messages:
    72
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #4
    Have posted more coding below, it is from the testimonials php page that displays the data from the database table



    <?php
    mysql_connect("", "", "") or die(mysql_error());
    mysql_select_db("") or die(mysql_error());
    $data = mysql_query("SELECT * FROM testimonials")
    or die(mysql_error());
    Print "<table border cellpadding=3>";
    while($testimonials = mysql_fetch_array( $data ))
    {
    Print "<tr>";
    Print "<th>Name:</th> <td>&nbsp;".$testimonials['Name'] . "<br></td></tr> ";
    Print "<th>Testimonial:</th> <td>&nbsp;".$testimonials['Testimonial'] . " <br><br></td></tr>";
    }
    Print "</table>";
    ?>
     
    ianhaney, Apr 14, 2013 IP
  5. jhine

    jhine Active Member

    Messages:
    25
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    53
    #5
    You need to read up on a few functions such as htmlspecialchars & strip_tags. I'd also recommend using MySQLi.
     
    jhine, Apr 14, 2013 IP
  6. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #6
    I've added a function to the script which will protect you from cross site scripting and sql injections. This isn't an ideal way to do this but at least this will fix the problem your'e having.

    
     
    <?php
    // clean function
    function clean($value,$html=TRUE,$mysql=TRUE) {    
       if($html) {    
           if(is_string($html)) {    
               $value = strip_tags($value, $html);        
           }else{
               $value = strip_tags(htmlentities($value));    
           }
       }
       if($mysql) {
           if(!get_magic_quotes_gpc()) {
               $value = stripslashes($value); 
           }
           $value = mysql_real_escape_string($value);     
       }
       return trim($value);
    }
     
    //=============Configuring Server and Database=======
    $host        =    '';
    $user        =    '';
    $password    =    '';
    //=============Data Base Information=================
    $database    =    '';
     
    $conn        =    mysql_connect($host,$user,$password) or die('Server Information is not Correct'); //Establish Connection with Server
    mysql_select_db($database,$conn) or die('Database Information is not correct');
     
    //===============End Server Configuration============
     
    //=============Starting Registration Script==========
     
    $name    =    clean($_POST['name']);
     
    $testimonial    =   clean($_POST['testimonial']);
     
    if(isset($_POST['btnRegister'])) //===When I will Set the Button to 1 or Press Button to register
    {
    $query    =    "insert into testimonials(name,testimonial)values('$name','$testimonial')";
    $res    =    mysql_query($query);
    header('location:feedbackconfirmation.php');
    }
    ?>
    
    PHP:
     
    HuggyStudios, Apr 14, 2013 IP
  7. ianhaney

    ianhaney Greenhorn

    Messages:
    72
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #7
    Thank you so much, works perfect and will have a read up on htmlspecialchars & strip_tags as well as MySQLi

    Thank you so much
     
    ianhaney, Apr 14, 2013 IP
  8. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #8

    Your'e welcome :D
     
    HuggyStudios, Apr 14, 2013 IP
  9. kingofdollars

    kingofdollars Well-Known Member

    Messages:
    97
    Likes Received:
    12
    Best Answers:
    1
    Trophy Points:
    110
    #9
    You should also use this technique for all other files too
    If you take input from user and put them directly in db you will risk your DB and your visitors too
     
    kingofdollars, Apr 17, 2013 IP