Jump to content

Pass variable with fixed value in mysql


biggieuk

Recommended Posts

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

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

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

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?

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 . "
";
   

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.