Jump to content

Passing a parameter into a query string


Tjk

Recommended Posts

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

Code in image create...
[code]

  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);
[/code]

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

Sure. Here it is....

[taken from imagecreate2.php]
[code]
<?
  $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);
?>
[/code]
Link to comment
Share on other sites

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

Ken
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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'] ?
Link to comment
Share on other sites

@ 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.
Link to comment
Share on other sites

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

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