biggieuk Posted January 16, 2009 Share Posted January 16, 2009 Hi all, Im trying to get this query working but im not sure if the syntax is correct: $query_sections = "SELECT * FROM tblsubsections WHERE imgsrc LIKE '%'".$value."'%'"; $sections = mysql_query($query_sections, $promo) or die(mysql_error()); $row_sections = mysql_fetch_assoc($sections); //update data into imgsrc $query_section = "UPDATE tblsubsections SET printareaimgsrc = '/images/printarea/'".$value."'.jpg' WHERE subsectionid = '".$row_sections['subsectionid']."'"; mysql_query($query_section, $promo) or die(mysql_error()); Could someone please tell me how to pass a variable into the query string with a fixed value infront of it? thanks Link to comment https://forums.phpfreaks.com/topic/141117-pass-variable-with-fixed-value-in-mysql/ Share on other sites More sharing options...
rhodesa Posted January 16, 2009 Share Posted January 16, 2009 $query_section = "UPDATE tblsubsections SET printareaimgsrc = '/images/printarea/{$value}.jpg' WHERE subsectionid = '{$row_sections['subsectionid']}'"; Link to comment https://forums.phpfreaks.com/topic/141117-pass-variable-with-fixed-value-in-mysql/#findComment-738581 Share on other sites More sharing options...
Maq Posted January 16, 2009 Share Posted January 16, 2009 You need to use brackets because the php variables don't get evaluated in single quotes. Link to comment https://forums.phpfreaks.com/topic/141117-pass-variable-with-fixed-value-in-mysql/#findComment-738585 Share on other sites More sharing options...
rhodesa Posted January 16, 2009 Share Posted January 16, 2009 You need to use brackets because the php variables don't get evaluated in single quotes. no..they were ending the string with a double quote...but there were extra single quotes in there effectively making the query: UPDATE tblsubsections SET printareaimgsrc = '/images/printarea/'somevalue'.jpg' WHERE subsectionid = '123' which won't work Link to comment https://forums.phpfreaks.com/topic/141117-pass-variable-with-fixed-value-in-mysql/#findComment-738590 Share on other sites More sharing options...
Maq Posted January 16, 2009 Share Posted January 16, 2009 My post was based off your answer... I was stating why you used the brackets. With double quotes you don't need brackets with singles you do. Link to comment https://forums.phpfreaks.com/topic/141117-pass-variable-with-fixed-value-in-mysql/#findComment-738593 Share on other sites More sharing options...
rhodesa Posted January 16, 2009 Share Posted January 16, 2009 My post was based off your answer... I was stating why you used the brackets. With double quotes you don't need brackets with singles you do. ah...always use them...makes variables easier to spot and keeps problems like this from coming up: print "Some array value: $array['value']"; which will error out Link to comment https://forums.phpfreaks.com/topic/141117-pass-variable-with-fixed-value-in-mysql/#findComment-738597 Share on other sites More sharing options...
biggieuk Posted January 16, 2009 Author Share Posted January 16, 2009 Thanks for your replies. I tried the following but my database is not updating. Can you spot any errors? p.s. 'db/conn.php' is used to setup the connection and works with other pages. <?php require_once('db/conn.php'); ?> <?php $arr = array("016B","025B","052B","064B","064P","066B","067B","067P","3MTM","ABFB","AKDB","AKFB","ALCB","ALFB","ALFP","ALGB","ALMB","ALMP","ALNB","ALSB","ALTB","AMQB","AMSR","APSB","ARSB","BASB","BCSB"); foreach ($arr as $value) { $query_sections = "SELECT * FROM tblsubsections WHERE imgsrc LIKE '%{$value}%' "; $sections = mysql_query($query_sections, $promo) or die(mysql_error()); $row_sections = mysql_fetch_assoc($sections); //update data into imgsrc $query_section = "UPDATE tblsubsections SET printareaimgsrc = '/images/printarea/{$value}.jpg' WHERE subsectionid = '{$row_sections['subsectionid']}'"; mysql_query($query_section, $promo) or die(mysql_error()); } ?> Any ideas? Link to comment https://forums.phpfreaks.com/topic/141117-pass-variable-with-fixed-value-in-mysql/#findComment-738611 Share on other sites More sharing options...
rhodesa Posted January 16, 2009 Share Posted January 16, 2009 i don't see anything wrong with it Link to comment https://forums.phpfreaks.com/topic/141117-pass-variable-with-fixed-value-in-mysql/#findComment-738614 Share on other sites More sharing options...
Maq Posted January 16, 2009 Share Posted January 16, 2009 Don't you want to loop through the results from the first query and use them for the second? Also, echo out your queries so you can see what's in them. $query_sections = "SELECT * FROM tblsubsections WHERE imgsrc LIKE '%{$value}%' "; echo $query_sections; $sections = mysql_query($query_sections, $promo) or die(mysql_error()); while($row_sections = mysql_fetch_assoc($sections)) { //update data into imgsrc $query_section = "UPDATE tblsubsections SET printareaimgsrc = '/images/printarea/{$value}.jpg' WHERE subsectionid = '{$row_sections['subsectionid']}'"; mysql_query($query_section, $promo) or die(mysql_error()); echo $query_section . " "; Link to comment https://forums.phpfreaks.com/topic/141117-pass-variable-with-fixed-value-in-mysql/#findComment-738618 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.