PHP : Display graph and o/p from form input on same page and sql query inside php
<p>I am dealing with some problem, I have a html page which does form input and takes two values (start date and end date) then using post method I post those values in sql statement which generates graph me. I am also trying to print some content from sql table i.e. i want to print avg of column of a table, I have tried      </p>

<p>Here is form input code:</p>

<pre><code>enter code here

&lt;html&gt;
&lt;body&gt;

&lt;form action="test.php" method="post"&gt;
Enter_Start_Date: &lt;input type="date" name="pkey1" /&gt;
Enter_End_Date: &lt;input type="date" name="pkey2" /&gt;
Enter_Type: &lt;input type="text" name="pkey3" /&gt;
Enter_Type: &lt;input type="text" name="pkey4" /&gt;
&lt;input type="submit" /&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Here is test.php which should display graph and print avg value on same page, when I execute this code it displays some garbage on webapge : </p>

<pre><code>&lt;html&gt;

&lt;body&gt;
&lt;?php
include("phpgraphlib.php");
$graph = new PHPgraphLib(1000,1000);
$host = 'host';
$port = '3306';
$server = $host . ':' . $port;
$user = 'root'; $password = 'root';

$link = mysql_connect ($server, $user, $password);

if (!$link)
{
    die('Error: Could not connect: ' . mysql_error());
}

$database = 'jira';

mysql_select_db($database);

$query = ("select pkey, numbervalue
from jiraissue ji
join customfieldvalue cf on ji.id = cf.issue
join resolution r on ji.resolution = r.id 
join issuestatus isst on ji.issuestatus = isst.id
join issuetype isstype on ji.issuetype = isstype.id
join label lb on ji.id = lb.issue
where numbervalue is not null
and isstype.pname in ('Ticket')
and lb.label in ('$_POST[pkey3]', ' $_POST[pkey4]')
and ji.updated between '$_POST[pkey1]' and '$_POST[pkey2]'");

$result = mysql_query($query);
if ($result) {
  while ($row = mysql_fetch_assoc($result)) {
      $customvalue=$row["pkey"];
      $count=$row["numbervalue"];
      //add to data array
      $dataArray[$customvalue]=$count;
  }
}

//configure graph
$graph-&gt;addData($dataArray);
$graph-&gt;setTitle("test");
$graph-&gt;setGradient("lime", "green");
$graph-&gt;setBarOutlineColor("black");
$graph-&gt;createGraph();
?&gt; 

&lt;?php 
include("phpgraphlib.php");
$graph = new PHPgraphLib(1000,1000);
$host = 'host';
$port = '3306';
$server = $host . ':' . $port;
$user = 'root'; $password = 'root';

$link = mysql_connect ($server, $user, $password);

if (!$link)
{
    die('Error: Could not connect: ' . mysql_error());
}

$database = 'jira';

mysql_select_db($database);

$query = ("select avg(numbervalue) as avg_hrs
        from jiraissue ji
        join customfieldvalue cf on ji.id = cf.issue
        join resolution r on ji.resolution = r.id
        join issuestatus isst on ji.issuestatus = isst.id
        join issuetype isstype on ji.issuetype = isstype.id
        join label lb on ji.id = lb.issue
        where numbervalue is not null
        and isstype.pname in ('Certification Ticket')
        and lb.label in ('$_POST[pkey3]', ' $_POST[pkey4]')
        and ji.updated between '$_POST[pkey1]' and '$_POST[pkey2]'");


$result = mysql_query($query);

if (!$result)
{
    $message = 'ERROR:' . mysql_error();
    return $message;
}
else
{
    $i = 0;
    echo ' ';
    while ($i &lt; mysql_num_fields($result))
    {
        $meta = mysql_fetch_field($result, $i);
        echo ' ' . $meta-&gt;name . ' ';
        $i = $i + 1;
    } echo ' ';

    $i = 0;
    while ($row = mysql_fetch_row($result))
    {
        echo ' '; $count = count($row);
        $y = 0;
        while ($y &lt; $count)
        {
            $c_row = current($row);
            echo ' ' . $c_row . ' ';
            next($row);
            $y = $y + 1;
        }
        echo ' ';
        $i = $i + 1;
    } echo ' ';
    mysql_free_result($result);
}

?&gt;


&lt;/body&gt;
&lt;/html&gt;
</code></pre>
