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
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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.