Jump to content


Photo

Mysql Update Statement problems


  • Please log in to reply
4 replies to this topic

#1 philjf1

philjf1
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 28 September 2003 - 12:42 PM

Hi there i wonder if any1 can help me with a real silly problem.

I am tryin to insert an image into a mysql cell which is type varchar(500)

Basically this is what i am trying to insert :

<img alt=\'$reason\' src=../images/purple/smallcup_gold.gif width=16 height=13>

as you can see this is standard html for inserting an image, the alt of the image being the awkuad part.

i am trying to keep the original data and add another image next to it.

so i tried this statement :

$sql = \"UPDATE ut2003_clans SET cups=\'$cup1 <img alt=\'\'$reason\'\' src=../images/purple/smallcup_gold.gif width=16 height=13>\' WHERE clan_tag=\'$clan\'\";

mysql_query($sql);

here are what the variables =

cup1 = whats previously in the cell
alt = hello this is a test


i have been playin for this for a few hours now and its racking my brain, the main problem being the alt tag on the image.

It simply will display only the 1st word in the variable for example if

$reason = hello this is a test

it will only display hello.


I tried some error checkin and found that if remove the $reason variable and replace it with \' \'hello this is a test\' \'

it works fine

but then if i try double quotes around the variable $reason, it wont even insert anything??


I hope some1 can understand this and point me in the right direction

thanking you

Phil

#2 pauper_i

pauper_i
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts
  • LocationPhoenix, AZ, USA

Posted 28 September 2003 - 01:32 PM

Hope you can follow my $reason-ing here ;):
OK, you are trying to stuff a variable\'s contents into a sql field. The problem is that the variable doesn\'t contain the quote marks you need to reference the entire string, so the first space encountered terminates the string.

Now play machine and read the following, but only where it is in matching quotes:
$sql = \"UPDATE ut2003_clans SET cups=\'$cup1 <img alt=
Now do you see the problem?
Try it this way instead:
[php:1:057979cdcc]<?php
$sql = \"UPDATE ut2003_clans SET cups=\'$cup1 <img alt=\"\".$reason.\"\" src=../images/purple/smallcup_gold.gif width=16 height=13>\' WHERE clan_tag=\'$clan\'\";
?>[/php:1:057979cdcc]
Incidentally, the quotes you had around $reason were not double quotes [\"] but two single quotes [\'][\'] - that can\'t have helped either, since the resulting machine-read string would then be some very poor HTML!!

Hope that helps!

D
Genius is one percent inspiration, and ninety-nine percent perspiration. [br]Thomas Alva Edison

#3 philjf1

philjf1
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 28 September 2003 - 01:52 PM

Thats superb mate, thanks very much

just 1 thing.

Could you explain what the \"\".$reason.\"\" actually do, i understand your comments about how the machine reads it, just not that bit above ;D

thanks again

phil

#4 pauper_i

pauper_i
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts
  • LocationPhoenix, AZ, USA

Posted 28 September 2003 - 02:36 PM

yep, no problem:
\"\".$reason.\"\"
would appear to the machine as
~\" + \"content of $reason\" + \"~
in other words, the backslash \'\' tells php to take the next charactre as is, not as a special character. As a consequence. the quote mark gets stored into the string along with all the text where previously it was being left out.
Think of the \'\' as being the escape character for PHP.

The period \'.\' is used in PHP to concatenate two strings, much like I used the \'+\' in the example above.

Hope that helps!

D
Genius is one percent inspiration, and ninety-nine percent perspiration. [br]Thomas Alva Edison

#5 philjf1

philjf1
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 28 September 2003 - 10:14 PM

great, thanks for your help again

nice forum!

Phil




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users