Advertisement

04.24.2008 at 08:10AM PDT, ID: 23350535
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

PHP Forum style text saving (addslashes/stripslashes/mysql_real_escape_string)

Asked by JackyKLL in PHP Scripting Language, Regular Expressions, WebApplications

Tags:

Okay... I got a code that required me to save text (come from textarea or text input via HTML form method POST) into files as php format. And I am required to load it back into my php code when needed.

Steps of the flow (saving):
1. Users type something into text area and send it to server
2. Server load the existing message (if there is one) and append the new message into array and store it (format as provided in Code Snippet)

My current setting:
1. Let php magic quote on (automatically addslashes applied)
2. stripslashes from $_POST/$_GET and apply mysql_real_escape_string

Current problems in my application:
if I have one message like a\'" (a slash singleQuote doubleQuote) the magic quote created this string a\\\'\", but everytime i load this into my php code the slashes infront of single quote will decreasing and the slash infront of double quote remain unchange. After 3 message saved (loaded the first message twice and saved twice) the inputed slash infront of single quote disappear, also the single quote terminated the string and crashed the code (array('a'\"', 'asda', 'asda', )).
e.g.  a\\\'\"' -> a\'\"' -> a'\"'

if I apply additional addslashes. the slash infront of my single quote will not disappear, but the slash for before double quotewill keep increasing
e.g. 'a\\\'\\\"' -> 'a\\\'\\\\\\\"' -> 'a\\\'\\\\\\\\\\\\\\\"'

if I used stripslashes on it, it can remove the additional slash infront of double quote but it also removed my input slash (the one infront of single quote)

Solution:
Either general method that forum use for saving text (in details that provide steps)
or a solution that solve my above problem ()Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
// 1. magic_quote
<?php $questions = array('1' => array('question' => 'ABC','type' => 'radio', 'answer' => array('a'\"', 'asda', 'asda', )),
'2' => array('question' => 'ABC','type' => 'radio', 'answer' => array('a\'\"', 'asda', 'asda', )),
'3' => array('question' => 'ABC','type' => 'radio', 'answer' => array('a\\\'\"'', 'asda', 'asda', ))); ?>
 
// 2. mysql_real_escape_string 
<?php $questions = array('1' => array('question' => 'ABC','type' => 'radio', 'answer' => array('a'\"\r\nasda\r\nasda', )),
'2' => array('question' => 'ABC','type' => 'radio', 'answer' => array('a\'\"\r\nasda\r\nasda', )),
'3' => array('question' => 'ABC','type' => 'radio', 'answer' => array('a\\\'\"\r\nasda\r\nasda', ))); ?>
 
// My method for saving message
    $contents = "<?"."php \$questions = array($nl";
      // Add existing messages through a loop
      if ($questions)
      {
        foreach ($questions as $question) {
          $answerIndex = 0;
          
          $answers = "'answer' => array("; 
          foreach($question["answer"] as $answer)
            $answers .= "'$answer', ";
          $answers .= ")";
          
          $contents .= "'$i' => array('question' => '".$question["question"].
                                      "','type' => '".$question["type"].            
                                      "', $answers),\n";
          $i += 1;
        }
      }
      
      $array = split("\r\n", $newQuestion["answer"]);
      $answers = "'answer' => array("; 
      foreach($array as $answer)
      {
        $answers .= "'$answer', ";
      }
      $answers .= ")"; 
      
      // Add the latest message by appending:
      $contents .= "'$i' => array('question' => '".$newQuestion["question"].
                                "','type' => '".$newQuestion["type"].
                                "', $answers)";
    // Close $messages array and PHP tag
    $contents .= "); ?".">";
[+][-]04.24.2008 at 08:17AM PDT, ID: 21431812

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP Scripting Language, Regular Expressions, WebApplications
Tags: PHP
Sign Up Now!
Solution Provided By: Chris_Gralike
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628