Welcome to Dream.In.Code
Become a PHP Expert!

Join 149,944 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,418 people online right now. Registration is fast and FREE... Join Now!




Inputing to MySQL and echoing it back

 
Reply to this topicStart new topic

Inputing to MySQL and echoing it back, help please!

brandon_v
28 Aug, 2008 - 04:39 PM
Post #1

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 26


My Contributions
What I have already is a script that will log in to MySQL, select the appropriate database, and then will save the information into that database. The input is retrieved from a textbox where the user types in whatever they want.

CODE
mysql_query("INSERT INTO posts (User, Content, Time, Date, postnum)
        VALUES ('" .$user. "', '" .$input. "', '" .$time. "' , '" .$date. "' , NULL)");

CODE
$result = mysql_query("SELECT * FROM posts");
    while ($row = mysql_fetch_array ($result))
      {
          echo $row['User'] . " said: <br />" . $row['Content'] . "<br /> at " .$row['Time'] . " on " . $row['Date'] . "<br />";
          echo "<br />";
      }


The problem is that whenever they enter a single quotation mark (') it doesn't write to my database.
Also, if they enter in a line break (enter), it will not echo it properly (although I do believe it is saving it properly). It will simply ignore the line break and put it on the same line.

Thanks in advance,
Brandon

User is online!Profile CardPM
+Quote Post

JackOfAllTrades
RE: Inputing To MySQL And Echoing It Back
28 Aug, 2008 - 06:23 PM
Post #2

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 862



Thanked: 89 times
Dream Kudos: 50
My Contributions
You should use mysql_real_escape_string on all user-supplied data prior to making a database query.
User is offlineProfile CardPM
+Quote Post

Moonbat
RE: Inputing To MySQL And Echoing It Back
28 Aug, 2008 - 06:25 PM
Post #3

D.I.C Regular
Group Icon

Joined: 30 Jun, 2008
Posts: 399



Thanked: 23 times
Dream Kudos: 600
My Contributions
If you had used mysql_real_escape_string() on the data given by the user, the quotes would have been automatically escaped with the \ character, and I'm sure it would have came out correctly when echoing it.
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Inputing To MySQL And Echoing It Back
28 Aug, 2008 - 06:45 PM
Post #4

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
the ' would end the query prematurely... also, you don't need to insert null, so just leave "postnum" out...

also, you can just use '$name' to insert instead of concatenation...
User is offlineProfile CardPM
+Quote Post

Ridikule
RE: Inputing To MySQL And Echoing It Back
28 Aug, 2008 - 07:48 PM
Post #5

D.I.C Head
**

Joined: 29 Sep, 2005
Posts: 58



Thanked: 4 times
My Contributions
As far as displaying the newlines correctly, look into the nl2br function.
User is offlineProfile CardPM
+Quote Post

brandon_v
RE: Inputing To MySQL And Echoing It Back
28 Aug, 2008 - 10:08 PM
Post #6

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 26


My Contributions
Thanks everyone, it works now.
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 04:56PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month