View Full Version : including text problem

11-05-2009, 08:24 PM
Hi, i have include.php

$culture = "Good luck searching! And thanks for coming.";

where i write masseges and they appear i my webpage.
But, faced problem when i want to write someting in " " or ' '. It splits my variable. How i can write don't or "music" without any problems?

I will be thenkfull if someone could tell me how to fix the same problem in my mysql inserting php script:

//connect to the database
$connect = mysql_connect("","","") or die("Error connecting to db");
//select table
mysql_select_db("") or die("Error selecting db");

$date = date("Y-m-d");
$time = date("H:i:s");
$type = $_POST['type'];

if($_POST['product'] == '') {
$error .= "<li>None or same product</li>";
if($_POST['about'] == '') {
$error .= "<li>Insert your description</li>";
if($_POST['url'] == '') {
$error .= "<li>Insert link</li>";
if(isset($_POST['submit'])) {
if(isset($error)) {
echo '<font color="#ff0000">Cant insert. Mistakes:';
echo '<ol>'.$error.'</ol></font>';
} else {
echo '<font color="Green">Inserted!</font>';
mysql_query("INSERT INTO this (product, about, date, time, url)
VALUES('$_POST[product]', '$_POST[about]', '$date', '$time', '$_POST[url]')") or die(mysql_error());


Thanks a LOT :)

11-05-2009, 08:59 PM
You need to escape the quotes.

//if you're using double-quotes to define the string,
//escape double-quotes inside the string
$string = "I said \"Hello\" to my brother's friend";

//if you're using single-quotes to define the string,
//escape single-quotes inside the string
$string = 'I said "Hello" to my brother\'s friend';

Just FYI:
Single-quotes have some advantage in parsing time, so I usually use them if the string is only text. In order to evaluate variables, however, you need to use double-quotes:

$name = 'Joe Smith';
$hiJoe = "Hi, $name"; // outputs: Hi, Joe Simth
$hiJoe = 'Hi, $name'; // wrong- outputs: Hi, $name

Your SQL problem is the array variables - MySQL doesn't like them. Assign the values to strings first:

$product = $_POST['product'];
$about = $_POST['about'];
$url = $_POST['url'];
mysql_query("INSERT INTO this (product, about, date, time, url)
VALUES('$product', '$about', '$date', '$time', '$url')")

11-05-2009, 09:11 PM
what if i want to use both " " and ' ' in my script?

11-08-2009, 02:16 AM
you can. (see my example.) For each $string, just make sure you escape ( \ ) whichever style quote you used to open and close it.

// string opened & closed with single quotes: escape all single quotes inside the string.
$single = 'I\'m using "quotes"';
// string opened & closed with double quotes: escape all double quotes inside the string.
$double= "I'm using \"quotes\"";