Log in

View Full Version : Resolved escaping quotes before reinsertion into database



james438
03-11-2010, 09:18 PM
If I retrieve a document from my database and assign it to $test, such as

$test=""hello"";

I am unable to reinsert it into the database. An error pops up. This is due to the fact that the quotes that were stored in the database can't be reinserted into the database without escaping the quotes so that it looks like:

$test="\"hello\"";

Now it is correct, however now I have slashes in my database field where previously there weren't any.

How can I reinsert the data into my database without the slashes?

BLiZZaRD
03-11-2010, 09:36 PM
It may be depreciated now, depends on your version of SQl ( a lot!) but there is the escape clause... so you could try:



Statement statement = // obtain reference to a Statement
statement.executeQuery(
"SELECT * FROM TEST WHERE TEST=/"Hello/"{escape '/'}");


Defining the escape character where noted.

Or you can always use single quote ' just use it twice... '' as in ''Hello'' instead of "Hello"

james438
03-11-2010, 11:55 PM
My first post was a bit unclear.

I am using MySQL 5.0


<?php
include 'include/dbstuff.php';
$query = "SELECT * FROM misc where ID=1";
$result = mysql_query($query,$conn) or die ("Couldn't execute query.");
while ($add_info = mysql_fetch_array($result)){
$ID = $add_info['ID'];
$summary = $add_info['summary'];
$query1 = "update misc set summary='$summary' where ID='$ID'";
mysql_query($query1) or die ("Couldnnt execute query.$query1");
}echo "all done";
?>

For example if column summary at row 1 contained the following: "hello,"he said. the above script would give me the error message


Couldnnt execute query.update misc set summary=''hello,' he said' where ID='1'

My mistake. Double quotes actually work fine.

As you can see this can be somewhat problematic when the content is a chapter from a book.

james438
03-12-2010, 01:10 AM
<?php
include 'include/dbstuff.php';
$query = "SELECT * FROM misc where ID=1";
$result = mysql_query($query,$conn) or die ("Couldn't execute query.");
while ($add_info = mysql_fetch_array($result)){
$ID = $add_info['ID'];
$summary = $add_info['summary'];
$summary = mysql_real_escape_string($summary);
$query1 = "update misc set summary='$summary' where ID='$ID'";
mysql_query($query1) or die ("Couldnnt execute query.$query1");
}echo "all done";
?>
Solves the problem.

james438
03-12-2010, 03:33 AM
My solution did not work quite as well as I thought. Slashes are still being added to the database with href="".

james438
03-12-2010, 05:27 AM
ok, I found the solution.

<?php
include 'include/dbstuff.php';
$query = "SELECT * FROM misc where ID=1";
$result = mysql_query($query,$conn) or die ("Couldn't execute query.");
while ($add_info = mysql_fetch_array($result)){
$ID = $add_info['ID'];
$summary = $add_info['summary'];
$summary = stripslashes($summary);
$summary = mysql_real_escape_string($summary);
$query1 = "update misc set summary='$summary' where ID='$ID'";
mysql_query($query1) or die ("Couldnnt execute query.$query1");
}echo "all done";
?>
It should be noted that
$summary = stripslashes($summary);
was used because magic-quotes-gpc (http://us3.php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc) was turned on. The fact that this is deprecated as of php 5.3 onwards and removed as of 6.0 onwards leads to another question, which I will ask in another thread (http://www.dynamicdrive.com/forums/showthread.php?t=53151).

fileserverdirect
03-13-2010, 12:00 AM
Is that a quadruple post? I'm pretty sure there's a rule against that, ONLY post if you are completely unsure on what to do to make sure you don't boost your post count. Instead, just edit the original post or whichever one came before.

james438
03-13-2010, 04:27 AM
How do you know that I was trying to artificially raise my post count? I think you are really misunderstanding what I was doing. You will notice that I actually have relatively few considering that I have been registered since early 2007. Many others have many more posts than myself and have been registered for only a few months. They had a lot of questions (or solutions), which is great! Most of the time I prefer to read the posts.

I edited all of the posts in that thread several times. The reason that I posted 4 consecutive times was the first time I felt I should clarify my first post as it was unclear. To edit my first post would be confusing to the casual reader.

The second of my consecutive posts was because I came up with a solution.

The third time was because after further testing I found that my solution did not work, so I stated what the problem was, since I was stumped, and hoped that someone could point me in the right direction.

The fourth and final time was because I came up with a solution and posted the answer. In my last post I also followed the rules and started a new thread for a related, but still different question, but I tried to be helpful and post a link to that new thread in case someone wanted to know more.

I see nothing wrong with posting the answer to my own questions and feel that it is important to be clear as to the questions as well as the solutions for others who are searching the web with a similar problem. The most common reason for me posting consecutively is when I have a question dealing with PCRE as it is not a favorite topic for people to answer and it can be rather tricky. When I post again in that same PCRE thread it is because I have something to add as far as progress towards a solution or the solution itself.

It is more important to me to be able contribute something of substance in my posts than to artificially raise my post count. Whether or not my posts do add to the discussion is another matter.

traq
03-13-2010, 04:58 AM
Is that a quadruple post? I'm pretty sure there's a rule against that...

...huh? are you serious?

who cares about artificially inflated post counts? I'm pretty sure James isn't a troll.

{edit #2} just to be on-topic, @james: yeah, I hate magic quotes.