Results 1 to 4 of 4

Thread: including text problem

  1. #1
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default including text problem

    Hi, i have include.php
    PHP Code:
    <?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:
    PHP Code:
    <?php
    //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

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    You need to escape the quotes.
    PHP Code:
    //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:
    PHP Code:
    $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:
    PHP Code:
    $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')"
    Last edited by traq; 11-05-2009 at 09:06 PM.

  3. #3
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    what if i want to use both " " and ' ' in my script?

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    you can. (see my example.) For each $string, just make sure you escape ( \ ) whichever style quote you used to open and close it.
    PHP Code:
    // 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\""

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •