Home | Community | Message Board

Out-Grow.com - Mushroom Growing Kits & Supplies
This site includes paid links. Please support our sponsors.


Welcome to the Shroomery Message Board! You are experiencing a small sample of what the site has to offer. Please login or register to post messages and view our exclusive members-only content. You'll gain access to additional forums, file attachments, board customizations, encrypted private messages, and much more!

North Spore Shop: Injection Grain Bag

Jump to first unread post Pages: 1
OfflineGypsy Boy
Redeemer
Male


Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 24 days
Need some help with PHP!!
    #27028831 - 11/08/20 03:31 PM (3 years, 2 months ago)

Run into difficulties, been busting my ass all day and figured out why my sql query didn't run.


INSERT INTO ftg (message)
VALUES ('Since JRR Tolkien published his mighty head-trip epic in the 1950s, its influence has got everywhere in popular culture: in games, the fantasy genre, and for a while of course in music. Peter Jackson's reverent screen version of Tolkien's first volume,');


Whats wrong with that statement? I'll tell u, it contains a " ' " and it corrupts the whole query!

What do i do?


--------------------





Edited by Gypsy Boy (11/08/20 04:37 PM)


Extras: Filter Print Post Top
OfflineGypsy Boy
Redeemer
Male


Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 24 days
Re: Need some MySQL help!! [Re: Gypsy Boy]
    #27028883 - 11/08/20 04:15 PM (3 years, 2 months ago)

Apparently when you DOUBLE quote VALUE with " instead of '  SQL query runs.

But when i put it into a PHP it gives me this error:

Quote:

Parse error: syntax error, unexpected '$message' (T_VARIABLE) in C:\wamp64\www\bug\ftg_action.php on line 23







Line 23: $sql = ("INSERT INTO ftg (message) VALUES ("$message")");

What gives???


--------------------





Extras: Filter Print Post Top
OfflineGypsy Boy
Redeemer
Male


Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 24 days
Re: Need some MySQL help!! [Re: Gypsy Boy]
    #27028983 - 11/08/20 05:35 PM (3 years, 2 months ago)

Been told to use prepared statements on stack overflow


--------------------





Extras: Filter Print Post Top
OfflineYthanA
ᕕ( ᐛ )ᕗ
Male User Gallery

Registered: 08/08/97
Posts: 18,774
Loc: NY/MA/VT Borderlands Flag
Last seen: 39 minutes, 15 seconds
Re: Need some help with PHP!! [Re: Gypsy Boy]
    #27029380 - 11/08/20 11:16 PM (3 years, 2 months ago)

Prepared statements are the way to go for queries because they help protect against SQL injection attacks and prevent sloppy bugs. But you should know how to use apostrophes/quotes in strings too. The problem you're encountering is that PHP sees the first apostrophe and thinks you're trying to end the string. Like, if you have this code:

Code:
$string = 'It's a test!';



What PHP sees is:

Code:
$string = 'It'



and then thinks everything after is invalid code.

You need to escape the character by adding a backslash so PHP knows to treat it as normal text and not as a string delimiter:

Code:
$string = 'It\'s a test!';



You could also use heredoc syntax which is useful for longer strings:

Code:
echo <<<AnyIdentifierYouWant
It's a test!
AnyIdentifierYouWant;



Extras: Filter Print Post Top
OfflineGypsy Boy
Redeemer
Male


Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 24 days
Re: Need some help with PHP!! [Re: Ythan]
    #27029855 - 11/09/20 09:26 AM (3 years, 2 months ago)

Quote:


$string = 'It\'s a test!';





My app/site gathers user comments, they cant be putting \ before every word like that! :wink:


Quote:


You could also use heredoc syntax which is useful for longer strings:





What do u mean by that, what's any identifier ?

How would u re write sql insert statment:

$sql = ("INSERT INTO ftg (message) VALUES ("$message")");

?

Thanks dude thanks for help much appreciate


--------------------





Extras: Filter Print Post Top
OfflineYthanA
ᕕ( ᐛ )ᕗ
Male User Gallery

Registered: 08/08/97
Posts: 18,774
Loc: NY/MA/VT Borderlands Flag
Last seen: 39 minutes, 15 seconds
Re: Need some help with PHP!! [Re: Gypsy Boy]
    #27029897 - 11/09/20 09:54 AM (3 years, 2 months ago)

Lol. Obviously you wouldn't depend on your users to properly escape their input. I was just explaining what was wrong with your code because you seemed confused. When programming, you'll frequently encounter the concept of having special characters that need to be escaped.

I would rewrite the SQL query using a prepared statement, as recommended on StackOverflow. There are functions like addslashes() and mysql_real_escape_string() but they're largely deprecated and I intentionally didn't mention them because they'll lead you down the wrong path.

I always try to sprinkle keywords like "heredoc syntax" in my replies so you can Google anything you find confusing. Good luck!


Extras: Filter Print Post Top
OfflineGypsy Boy
Redeemer
Male


Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 24 days
Re: Need some help with PHP!! [Re: Ythan]
    #27030235 - 11/09/20 01:17 PM (3 years, 2 months ago)

Hey,

$sql = ("INSERT INTO ftg (message) VALUES (\"$message\")");

works!

Now i just need to figure out how to do "prepared statements" !


--------------------





Extras: Filter Print Post Top
Jump to top Pages: 1

North Spore Shop: Injection Grain Bag


Similar ThreadsPosterViewsRepliesLast post
* String Theory and Quantum Physics.
( 1 2 all )
ergot 5,216 24 01/03/04 01:02 AM
by MarioNett
* String Theory Silversoul 1,128 8 10/02/05 02:41 PM
by DieCommie
* webmasters HELP ( php ) ChromeCrow 713 3 09/08/03 07:47 AM
by Seuss
* Monkeys 'string words together' DiploidM 1,017 14 05/29/06 09:23 AM
by relativexistance
* String Theory dblaney 1,878 10 05/18/05 04:45 PM
by dblaney
* PHP vs. Cold Fusion Evolving 1,126 7 03/15/03 06:08 AM
by Lana
* PHP NUKE RIPPING Genius 603 3 11/06/04 05:11 PM
by Ythan
* PHP coders wanted ThePredator 898 10 09/28/05 04:46 PM
by automan

Extra information
You cannot start new topics / You cannot reply to topics
HTML is disabled / BBCode is enabled
Moderator: trendal, automan, Northerner
442 topic views. 0 members, 0 guests and 3 web crawlers are browsing this forum.
[ Show Images Only | Sort by Score | Print Topic ]
Search this thread:

Copyright 1997-2024 Mind Media. Some rights reserved.

Generated in 0.025 seconds spending 0.007 seconds on 14 queries.