Jump to content


Photo

Passing a parameter into a query string


  • Please log in to reply
9 replies to this topic

#1 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 10 June 2006 - 10:20 AM

Code in main page...
<?
$var= 1;
?>
<form method="POST" action="map.php">
<input type="image" src="imagecreate2.php?var='$var'" height="300" width= "300" />
</form>

Code in image create...

  if(isset($_GET['var'])){
    $var= $_GET['var'];
  }

  mysql_query("SELECT * from table WHERE id='$var'") or die(mysql_error());

//does something to an image and outputs it using imagepng($img);

The problem is that the image now doesn't load on the main page. Could anyone point out where I'm going wrong?

Regards
-Tjk

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 June 2006 - 12:01 PM

Can you post the code that sends the image back to the browser. You are probably missing the header() fucntion that sets the correct output type.

Ken

#3 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 10 June 2006 - 01:55 PM

Sure. Here it is....

[taken from imagecreate2.php]
<?
  $im= imagecreatefrompng('maptemp.png');
  
  if(isset($_GET['battle'])){
    $battle= $_GET['battle'];
  }
  
  mysql_connect("localhost", "user", "password")or die(mysql_error());
  mysql_select_db("database");
  
  $query= mysql_query("SELECT * from battles WHERE battle='$battle'")or die(mysql_error());
  while($row= mysql_fetch_array($query)){
    $unit= $row['unit'];
    $unitx= $row['xcoord'];
    $unity= $row['ycoord'];
    
    
    $lcoordx= $unitx- 5;
    $lcoordy= $unity - 5;
  
    $rcoordx= $unitx + 5;
    $rcoordy= $unity + 5;
    
    $color= imagecolorallocate($im, 0xFF, 0xFF, 0xFF);  
    imagefilledrectangle($im, $lcoordx, $lcoordy, $rcoordx, $rcoordy, $color);
  } 
  imagepng($im);
?>


#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 June 2006 - 04:46 PM

You're missing the "header()" function that tells the web browser to expect image data:
Add the following line to your funtion
<?php    header('Content-type: image/png'); ?>
before the line
<?php imagepng($im); ?>

Ken


#5 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 10 June 2006 - 07:11 PM

I've added the header() function just above imagepng($im) and it doesn't work.

Before I added the parameter into the query string for imagecreate2.php when I was just using a mysql statement with no WHERE condition everything was working fine and the map loaded. It was only when I added the parameter and to allow for the WHERE condition that it no longer works. Any ideas?

Help appreciated as always.

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 10 June 2006 - 07:18 PM

i don't see in your script where you are passing the battle variable to your imagecreate2.php script. it seems as though when you do this:

if(isset($_GET['battle'])){
$battle= $_GET['battle'];
}

$_GET['battle'] is not set, and therefore $battle is never set, either, so when you do your query, it selects nothing.

edit: did you mean to do $_GET['var'] ?

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 10 June 2006 - 07:22 PM

if it's purely a query page do this
if (isset ($_GET['battle'])) $battle = $_GET['battle']; else die ("Missing Variables");

or just incapsulate the entire code in the $_GET['battle'] if() function
z..z..z..z..z..z..z..z..

#8 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 10 June 2006 - 08:01 PM

@ Crayon Violent: Earlier in the script the variable $battle is defined. Sorry, the original code I posted was a test script I set up to test if it was something wrong with my code. Everything is identical except in my example I used $var instead of $battle. $battle is definately set within the main script and ready to pass via the query string into the image creation script.

I'm using a script called 'map.php' and through the img src I'm calling the script imagecreate2.php to generate the image.

@joquius: I need the variable $battle to be set at all times so an else isn't really an option.

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 June 2006 - 08:32 PM

I just took a closer look at the original code you posted:
<?php
$var= 1;
?>
<form method="POST" action="map.php">
<input type="image" src="imagecreate2.php?var='$var'" height="300" width= "300" />
</form>
Notice that the "<input>" line is outside the PHP tags, so you are passing the literal string "'$var'" to your script, not the value of [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$var[!--colorc--][/span][!--/colorc--].
What you really should have there is:
<input type="image" src="imagecreate2.php?var=<?php echo $var; ?>" height="300" width= "300" />
If you look at the generated source before and after you've made this change, you should see the difference.

Ken

#10 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 11 June 2006 - 06:40 AM

ooh good eye, [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users