Results 1 to 25 of 25

Thread: stripslashes cut off data

  1. #1

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Resolved stripslashes cut off data

    This is supposed to be simple, but still.....

    When saving a value to the database I use addslashes (so test'er become test\'er) ... no problem there.

    But when I read it back I get this....
    echo $row->$value = tes
    echo stripslashes($row->$value) = tes

    uuuurgh..drive me bonkers. I also checked get_magic_quotes_gpc(), and then add or stripslashes.
    Dont want to edit my php.ini as I'm not sure where I will host this site yet.
    Last edited by StrangerInBeijing; Jul 4th, 2008 at 11:40 AM.
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: stripslashes cut off data

    can you post the code you use to put it into the db?
    My usual boring signature: Something

  3. #3

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    The
    Code:
    $set .= $value."='".addslashes($_POST[$value][$count])."',";
    in
    Code:
    	function update_continent($columns,$count)  {		
    		$id = '';
    		$set = '';
    		foreach ($columns as $value)  {
    			if($value=='id') {
    				$id = $_POST[$value][$count];
    			} else {
    				$set .= $value."='".addslashes($_POST[$value][$count])."',";		
    			}
    		}
    		//get the translation id for this continet
    		$sql = "SELECT translation_id FROM continents WHERE id=".$id;
    		$results =  $this->conn->query($sql);
    		$record = $results->fetch_array();
    		$translation_id=$record['translation_id'];
    		$set = substr($set,0,strlen($set)-1);
    		$sql = "UPDATE translations SET ".$set." WHERE id=".$translation_id;
    		$this->conn->query($sql);		
    	}
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  4. #4

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    oh oh...I just realized that that build a sql string (whre there's sth to escape) like this:
    UPDATE translations SET en='tes\\\'er',af='aaa' WHERE id=1439
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  5. #5
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: stripslashes cut off data

    i dont see any problem with it. Wait to have penagate or manavo look at it. they might see something wrong.

    sorry!
    My usual boring signature: Something

  6. #6

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    Mendhaaaaaaaakkkkkkk!!!!!!!!!
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  7. #7
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: stripslashes cut off data

    haha no not mendhak. he is a asp.net guru. good luck
    My usual boring signature: Something

  8. #8

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    Mendhak knows everything....conversation over.

    For whoever comes along to check this out....
    1) For what it matter, I'm working on a WinXP machine with WAMP Server (not sure if this got sth to do with add / strip slashes
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  9. #9
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: stripslashes cut off data

    Have you tried using mysql_real_escape_string?
    » Twitter: @rudi_visser : Website: www.rudiv.se «

    If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: stripslashes cut off data

    That function is so confusing I don't even know what it's supposed to do, let alone why it's not doing it.

    Make it simpler. Pass in the fields as a hashtable. Run mysql_real_escape_string on each value as you build the query. Don't escape anything before this final step.

    You may want to reverse the effect of magic quotes at the start of the script.
    PHP Code:
    if (@get_magic_quotes_gpc())
    {
      
    array_walk_recursive($_GET'stripslashes');
      
    array_walk_recursive($_POST'stripslashes');
      
    array_walk_recursive($_COOKIE'stripslashes');


  11. #11

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    My function?
    It's pretty cool actually (the idea..sure anyone can code it better).
    Bottom line is when a language get "activated", the code check if a column for that language exists in the translation table, and if not create it.
    So for whatever insert/update code I write I can never know exactly how many columns there are, so had to come up with a "different" approach.

    Back to the slash thing. Let me try your code.
    Weird thing I just realized is that on another page I also query to get the value, without using stripslases and it display just fine (just a string i echo), but on the "edit" page where i show the value in a text box.....cut off.

    Will fiddle around using this array_walk thing you just send me (yeah, php dumbnut me)
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  12. #12

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    NO bloody hell.

    I check for magic quotes using "if (@get_magic_quotes_gpc())" and use "addslashes(" before saving the value to the database.
    This works fine as in phpmyadmin i see "Lao People\'s Republic"

    Reading it back is the b!tch!!!!!
    Once again use "if (@get_magic_quotes_gpc())" and use "stripslashes(.." but the above will show "Lao People".
    If not using stripslashes, it show "Lao People\" ... why so complicated?

    Why they dont just build this into msqli so we dont have to worry abut it....uurgh
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  13. #13

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    Quote Originally Posted by RudiVisser
    Have you tried using mysql_real_escape_string?
    same result
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  14. #14
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: stripslashes cut off data

    Quote Originally Posted by StrangerInBeijing
    This works fine as in phpmyadmin i see "Lao People\'s Republic"
    No, no, no. The backslash must not go into the database.


    Quote Originally Posted by StrangerInBeijing
    Why they dont just build this into msqli so we dont have to worry abut it....uurgh
    If you are using mysqli, use parameters so that you don't have to worry about this nonsense.

  15. #15

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    The bit I know I learned from a book, which suggested mysqli. Seeing it's "newer" I sticked to it, but don't know what you mean use parameters.
    Mind giving me a push in right direction?
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  16. #16

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    You mean this? (just googled) <?php
    Code:
    $mysqli = new mysqli('localhost', 'user', 'password', 'world');
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    $stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
    $stmt->bind_param('sssd', $code, $language, $official, $percent);
    
    $code = 'DEU';
    $language = 'Bavarian';
    $official = "F";
    $percent = 11.2;
    
    /* execute prepared statement */
    $stmt->execute();
    
    printf("%d Row inserted.\n", $stmt->affected_rows);
    
    /* close statement and connection */
    $stmt->close();
    
    /* Clean up table CountryLanguage */
    $mysqli->query("DELETE FROM CountryLanguage WHERE Language='Bavarian'");
    printf("%d Row deleted.\n", $mysqli->affected_rows);
    
    /* close connection */
    $mysqli->close();
    ?>
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  17. #17
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: stripslashes cut off data

    Yeah, that's the one.

  18. #18

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    so no stripslashes, addslashes, real_escape_string or any of that no more?
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  19. #19
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: stripslashes cut off data

    You may want to keep the code that runs stripslashes if magic quotes are enabled. Don't add slashes into parameter variables.

  20. #20

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    Dont know how to use that in a scenario like this.
    Thing is I cannot know how many columns there will be in the table. A column for a language get added when a language are activated.

    My code use the following function to add a record, by passing 2 arrays, one with the column names that were determined ealier that will look like:
    [0]='id', [1]='en',[2]='zh' and so on (this will be the columns currently in the translation table.
    The second array is the values and will look like [en]='English Value',[zh]='Chinese Value, and so on.
    I call the following function passing this two arrays:

    Code:
    function add_translation($columns,$post)  {
    	//start the query string
    	$sql = "INSERT INTO translations(";
    	//add the columns to which data will be written
    	foreach ($columns as $value)  {
    		if($value!='id')
    			$sql .=$this->slash($value).",";
    	}
    	//remove the comma that will be at end and add bracket
    	$sql = substr($sql,0,strlen($sql)-1).") VALUES(";
    	//add the values to the query string
    	foreach ($columns as $value)  {
    		if($value!='id')
    			$sql.= "'".$post[$value]."',";
    	}
    	//remove the comma that will be at end and add bracket
    	$sql = substr($sql,0,strlen($sql)-1).")";
    	//excecute and return new id
    	$this->conn->query($sql);
    	return mysqli_insert_id($this->conn);
    }
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  21. #21

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    maybe i should do sth like this? (does not work though)

    idea is do one value at a time, starting with and insert (as the en (english) column and id column will always be present.

    Code:
    	function add_translation($columns,$post)  {
    		//we will always have english, so use english value to CREATE new record
    		$stmt = $this->con->prepare("INSERT INTO translations VALUES (?)");
    		$stmt->bind_param('s', $lang);
    		$lang = $post['en'];
    		$stmt->execute();
    		if($stmt->affected_rows < 1)
    			return 'could not insert';
    		$newid = mysqli_insert_id($this->conn);
    		foreach ($columns as $value)  {
    			if($value!='en' && $value!='id')  {
    				$stmt = $this->con->prepare("UPDATE translations SET ".$value." =? WHERE id=?");
    				$stmt->bind_param('si',$value,$newid);
    				$stmt->execute();
    				$stmt->close();				
    			}
    		}
    	}
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  22. #22

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    trying the old way again, but going insane....

    mysqli_real_escape_string($this->conn, $post[$value]) ... this insert "aaa's" as "aaaa\'s" in the database. (which they say is wrong).....so no matter how one try to read it back, you never get it right.
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  23. #23

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    seems you are wrong penegate

    The following function works fine:
    Code:
    function add_translation($value)  {
    	$stmt = $this->conn->prepare("INSERT INTO translations (en) VALUES (?)");
    	$stmt->bind_param('s', $value);
    	/* execute prepared statement */
    	$stmt->execute();
    	/* close statement and connection */
    	$stmt->close(); 
    	return mysqli_insert_id($this->conn);
    }
    It will save "aaa's" as "aaa\'s" in the database.
    And if I read that back without stripslashes I get "aaa\", while with stripslashes I get "aaa"
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  24. #24

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    oh god..all this time wasted.
    check this out.....
    if if try display the value as above inside a text box it does that.
    when i display it as text on a page it shows correctly
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  25. #25

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: stripslashes cut off data

    hahhaahahhahahahaha

    changed
    Code:
    <input name=".$value."[] type='text' id=".$value." value='".stripslashes($row->$value)."' />
    to
    Code:
    <input name=".$value."[] type='text' id=".$value." value=".stripslashes($row->$value)." />
    fine!!!!!!!!!!!!!
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width