Inserting Variables Into Databases

Discussion in 'PHP' started by SmileServe, Aug 12, 2007.

  1. #1
    Hi,

    The following code inserts the literal variables (physically displays variables on website, eg. $title) rather than displaying the result of the variables. How can this be corrected?

    {
    		@mysql_query('INSERT INTO jos_content (title, title_alias, introtext, state, sectionid, mask, catid, created, created_by, created_by_alias, modified, modified_by, checked_out, checked_out_time, publish_up, publish_down, images, urls, attribs, version, parentid, ordering, metakey, metadesc, access, hits) VALUES (\'$title\',\'$title\',\'<object type="application/x-shockwave-flash" data="player.swf" '
            . ' width="320" height="262" id="FlowPlayer">'
            . ' <param name="allowScriptAccess" value="sameDomain" />'
            . ' <param name="movie" value="http://tvloser.com/videos/$md5.flv">'
            . ' <param name="quality" value="high">'
            . ' <param name="scale" value="noScale">'
            . ' <param name="wmode" value="transparent">'
            . ' <param name="flashvars" value="config={videoFile:"http://tvloser.com/videos/$md5.flv",autoPlay: true}">'
            . ' </object></textarea>'
            . ' </div>\', \'1\', \'2\', \'0\', \'3\', \'2007-08-10 11:54:06\', \'62\', \'\', \'0000-00-00 00:00:00\', \'62\', \'0\', \'0000-00-00 00:00:00\', \'2007-08-10 11:54:06\', \'0000-00-00 00:00:00\', \'\', \'\', \'pageclass_sfx='
            . ' back_button='
            . ' item_title=1'
            . ' link_titles='
            . ' introtext=1'
            . ' section=0'
            . ' section_link=0'
            . ' category=0'
            . ' category_link=0'
            . ' rating='
            . ' author='
            . ' createdate='
            . ' modifydate='
            . ' pdf='
            . ' print='
            . ' email='
            . ' keyref='
            . ' docbook_type='
            . ' html_title=$title'
            . ' robots=-1'
            . ' google_cache=1'
            . ' google_snippet=1\', \'2\', \'0\', \'1\', \'METAKEY FIELD\', \'META DESC FIELD\', \'0\', \'0\')');
    	}
    	
    }
    PHP:
    Thanks.
     
    SmileServe, Aug 12, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Variables are not parsed between single quotes. Use double quotes, or do:
    
    $query = 'INSERT INTO [...] (\'' . $title .'\', [...]';
    
    PHP:
     
    nico_swd, Aug 13, 2007 IP
  3. SmileServe

    SmileServe Well-Known Member

    Messages:
    1,121
    Likes Received:
    64
    Best Answers:
    0
    Trophy Points:
    155
    #3
    Thanks , that worked for $title however I'm not sure about these two bits of code:

    . ' <param name="movie" value="http://tvloser.com/videos/$md5.flv">'
    PHP:
    . ' <param name="flashvars" value="config={videoFile:"http://tvloser.com/videos/$md5.flv",autoPlay: true}">'
    PHP:
     
    SmileServe, Aug 13, 2007 IP
  4. l0gic

    l0gic Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Same deal. You just have to remove the variables from within the string, like this:
    . ' <param name="movie" value="http://tvloser.com/videos/' . $md5 . '.flv">'
    PHP:
    . ' <param name="flashvars" value="config={videoFile:"http://tvloser.com/videos/' . $md5 . '.flv",autoPlay: true}">'
    PHP:
     
    l0gic, Aug 13, 2007 IP
  5. SmileServe

    SmileServe Well-Known Member

    Messages:
    1,121
    Likes Received:
    64
    Best Answers:
    0
    Trophy Points:
    155
    #5
    Hi,

    Thanks very much - that works.

    However the following code is causing an issue:

    . ' <param name="flashvars" value="config={videoFile:"http://tvloser.com/videos/' . $md5 . '.flv",autoPlay: true}">'
    PHP:
    In order for the flash player to work the output must be in the format videoFile:'http://domain.com/file.flv' , with single quotes rather than double.

    I've attempted to correct this but can't quite get the syntax correct.
     
    SmileServe, Aug 13, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    nico_swd, Aug 13, 2007 IP
  7. SmileServe

    SmileServe Well-Known Member

    Messages:
    1,121
    Likes Received:
    64
    Best Answers:
    0
    Trophy Points:
    155
    #7
    Hi,

    I tried the following and the SQL insert appears to fail as nothing is inserted.

    . ' <param name="flashvars" value="config={videoFile:\'http://tvloser.com/videos/' . $md5 . '.flv\',autoPlay: true}">'
    PHP:
    The php.net link isn't loading for me either.
     
    SmileServe, Aug 13, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    I fixed the URL above.

    And let MySQL tell you what the issue is, by adding OR die(mysql_error()); at the end of the query.

    
    mysql_query("INSERT INTO [....]") OR die(mysql_error());
    
    PHP:
     
    nico_swd, Aug 13, 2007 IP
  9. SmileServe

    SmileServe Well-Known Member

    Messages:
    1,121
    Likes Received:
    64
    Best Answers:
    0
    Trophy Points:
    155
    #9
    Hi,

    OK, gives the error:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's Head Dents Wall','Kid's Head Dents Wall','<object type="application/x-shockwav' at line 1

    Here's the current code for the SQL insert:

    mysql_query('INSERT INTO jos_content (title, title_alias, introtext, state, sectionid, mask, catid, created, created_by, created_by_alias, modified, modified_by, checked_out, checked_out_time, publish_up, publish_down, images, urls, attribs, version, parentid, ordering, metakey, metadesc, access, hits) VALUES (\'' . $title .'\',\'' . $title .'\',\'<object type="application/x-shockwave-flash" data="http://tvloser.com/player.swf" '
            . ' width="320" height="262" id="FlowPlayer">'
            . ' <param name="allowScriptAccess" value="sameDomain" />'
            . ' <param name="movie" value="http://tvloser.com/videos/' . $md5 . '.flv">'
            . ' <param name="quality" value="high">'
            . ' <param name="scale" value="noScale">'
            . ' <param name="wmode" value="transparent">'
            . ' <param name="flashvars" value="config={videoFile:\'http://tvloser.com/videos/' . $md5 . '.flv\',autoPlay: true}">'
            . ' </object></textarea>'
            . ' </div>\', \'1\', \'2\', \'0\', \'3\', \'2007-08-10 11:54:06\', \'62\', \'\', \'0000-00-00 00:00:00\', \'62\', \'0\', \'0000-00-00 00:00:00\', \'2007-08-10 11:54:06\', \'0000-00-00 00:00:00\', \'\', \'\', \'pageclass_sfx='
            . ' back_button='
            . ' item_title=1'
            . ' link_titles='
            . ' introtext=1'
            . ' section=0'
            . ' section_link=0'
            . ' category=0'
            . ' category_link=0'
            . ' rating='
            . ' author='
            . ' createdate='
            . ' modifydate='
            . ' pdf='
            . ' print='
            . ' email='
            . ' keyref='
            . ' docbook_type='
            . ' html_title=$title'
            . ' robots=-1'
            . ' google_cache=1'
            . ' google_snippet=1\', \'2\', \'0\', \'1\', \'METAKEY FIELD\', \'META DESC FIELD\', \'0\', \'0\')')OR die(mysql_error());;
    PHP:
     
    SmileServe, Aug 13, 2007 IP
  10. SmileServe

    SmileServe Well-Known Member

    Messages:
    1,121
    Likes Received:
    64
    Best Answers:
    0
    Trophy Points:
    155
    #10
    nico_swd - For some reason your last post had dissapeared when I browsed back to it.

    From what I read when the post was visible you were saying that the values at the end of my query shouldn't be escaped.

    So the code is now as follows:

    mysql_query('INSERT INTO jos_content (title, title_alias, introtext, state, sectionid, mask, catid, created, created_by, created_by_alias, modified, modified_by, checked_out, checked_out_time, publish_up, publish_down, images, urls, attribs, version, parentid, ordering, metakey, metadesc, access, hits) VALUES (\'' . $title .'\',\'' . $title .'\',\'<object type="application/x-shockwave-flash" data="http://tvloser.com/player.swf" '
            . ' width="320" height="262" id="FlowPlayer">'
            . ' <param name="allowScriptAccess" value="sameDomain" />'
            . ' <param name="movie" value="http://tvloser.com/videos/' . $md5 . '.flv">'
            . ' <param name="quality" value="high">'
            . ' <param name="scale" value="noScale">'
            . ' <param name="wmode" value="transparent">'
            . ' <param name="flashvars" value="config={videoFile:\'http://tvloser.com/videos/' . $md5 . '.flv\',autoPlay: true}">'
            . ' </object></textarea>'
            . ' </div>\', \'1\', \'2\', \'0\', \'3\', \'2007-08-10 11:54:06\', \'62\', \'\', \'0000-00-00 00:00:00\', \'62\', \'0\', \'0000-00-00 00:00:00\', \'2007-08-10 11:54:06\', \'0000-00-00 00:00:00\', \'\', \'\', \'pageclass_sfx='
            . ' back_button='
            . ' item_title=1'
            . ' link_titles='
            . ' introtext=1'
            . ' section=0'
            . ' section_link=0'
            . ' category=0'
            . ' category_link=0'
            . ' rating='
            . ' author='
            . ' createdate='
            . ' modifydate='
            . ' pdf='
            . ' print='
            . ' email='
            . ' keyref='
            . ' docbook_type='
            . ' html_title=$title'
            . ' robots=-1'
            . ' google_cache=1'
            . ' google_snippet=1', '2', '0', '1', 'METAKEY FIELD', 'META DESC FIELD', '0', '0')')OR die(mysql_error());;
    PHP:
    This produces the following error:

    Parse error: syntax error, unexpected ''' in /home/tvloser/public_html/videos.php on line 296
     
    SmileServe, Aug 13, 2007 IP
  11. SmileServe

    SmileServe Well-Known Member

    Messages:
    1,121
    Likes Received:
    64
    Best Answers:
    0
    Trophy Points:
    155
    #11
    Anymore ideas anyone?
     
    SmileServe, Aug 13, 2007 IP