Tengo un script para generar la vuelo un grafico de barras fruto de una encuesta
Todo va bien mientras register_global esta en on , pero si digo de cambiarlo las votaciones no suman. Esto si se por que ocurre, ya que al no traspazarle la variable que le "dice" que se ha votado logicamente no lo registra en la base
Bien pense que con register_global en off podria hacer uso de $_post y asi tomar el valor de la variable vote para que si registrara la votacion
osea, if (!empty($_POST["vote"])) en vez de if (!empty($vote))
pero aqui viene el problema ya que me da el error
No se puede mostrar la imagen localhost/encuesta_mia/show_poll.php porque contiene errores.
he probado de varias formas pero en cuanto le agrego algo al codigo me sale el error
os pego el html y php
=================== HTML =========================
<html>
<head><title>Encuenta sobre sistema operativo</head>
<body>
<h1>Sondeo</h1>
<h2>Sondeo para la eleccion de sistema operativo</h2>
<h3>Elija el sistema operativo de su preferencia</h3>
<form method="POST" action="show_poll.php">
<input type="radio" name="vote" value="windows">Windows<br>
<input type="radio" name="vote" value="linux" checked>Linux<br>
<input type="radio" name="vote" value="solaris">Solaris<br><br>
<input type="submit" value="enviar encuesta">
</form>
</body>
</html>
===================== PHP ========================
<?php
//*** ************************************************** *******************
// PETICION A LA BASE DE DATOS DE LOS DATOS PARA REALIZAR LA ENCUENTA
//************************************************** ******************************
if (!$db_connect=mysql_connect("localhost","coso","10 49gg"))
{
echo "No se ha podido conectar a la base de datos<br>";
exit;
}
@mysql_select_db("encuesta");
if (!empty($vote))
// si se ha votado
{
$vote=addslashes($vote);
$query="UPDATE poll_results
SET num_votes=num_votes+1
WHERE candidate='$vote' ";
if (!mysql_query($query,$db_connect))
{
echo "No se ha podido conectar a la base de datos<br>";
exit();
}
};
// obtener los resultados actuales de las encuesta,
// independientemente de los que se haya votado
$query="SELECT * FROM poll_results";
if (!($result=mysql_query($query,$db_connect)))
{
echo "No se ha podido conectar a la base de datos<br>";
exit;
}
// cantidad de candidatos
$num_candidates=mysql_num_rows($result);
// calcular el numero de votos hasta ahora
$total_votos=0;
while ($row=mysql_fetch_object($result))
{
$total_votos+=$row->num_votes;
}
// resetea $result y pone el puntero interno en primer lugar
mysql_data_seek($result,0);
//************************************************** **************************
// CALCULO INCIAL PARA EL GRAFICO
//************************************************** **************************
$width=500; //ancho de la imagen en pixeles.Encajara en 640x480
$left_margin=50; // margen izquierdo
$right_margin=50; // margen derecho
$bar_height=40;//alto de la barra
$bar_spacing=$bar_height/2;//separacion entre las barras
$font="arial.ttf";
$title_size=16;
$main_size=12;
$small_size=12;
$text_indet=10; // posicion para las etiquetas de texto a la izquierda
// configurar el punto inicial desde el cual dibujar
$x=$left_margin+60;
$y=50;
$bar_unit=($width - ($x+$right_margin)) /100;// un "punto" centecima parte en la barra
//calcula el alto total del grafico
$height=$num_candidates*($bar_height+$bar_spacing) +50;
//************************************************** **************************
// CONFIGURAR LA IMAGEN BASE
//************************************************** **************************
//crear un lienzo en blanco para la imagen
$im=imagecreate($width,$height);
//asignar colores
$white=imagecolorallocate($im,255,255,255);
$blue=ImageColorAllocate($im,0,64,128);
$black=ImageColorAllocate($im,0,0,0);
$pink = ImageColorAllocate($im,255,78,243);
$text_color=$black;
$percent_color=$black;
$bg_color=$white;
$line_color=$black;
$bar_color=$blue;
$number_color=$pink;
//crear un rectangulo relleno color blanco para dibujar encima
imagefilledrectangle($im,0,0,$width,$height,$bg_co lor);
// dibujar un borde entorno al lienzo
imagerectangle($im,0,0,$width-1,$height-1,$line_color);
// añadir titulo
$title="Resultados del sondeo";
$title_dimensions = ImageTTFBBox($title_size, 0, $font, $title);
$title_length = $title_dimensions[2] - $title_dimensions[0];
$title_height = abs($title_dimensions[7] - $title_dimensions[1]);
$title_above_line = abs($title_dimensions[7]);
$title_x=($width - $title_length) /2; // centrando el texto del titulo en x
$title_y=($y - $title_height) /2 + $title_above_line; // lo mismo la y
imagettftext($im,$title_size,0,$title_x,$title_y,$ text_color,$font,$title);
imageline($im,$x,$y-5,$x,$y-15,$line_color);
/*******************************************
DIBUJAR LOS DATOS DEL GRAFICO
*******************************************/
// se extrae cada fila de la base y se dibujan la barra correspondiente
while ($row=mysql_fetch_object($result))
{
if($total_votos>0)
{
$percent=intval(round( ($row->num_votes / $total_votos)*100 ) );
}
else
{
$percent=0;
}
//muestra el tanto por ciento en numero
imagettftext($im,$main_size,0,$width-30,$y+($bar_height/2),$percent_color,$font,$percent."%");
if($total_votos>0)
{
$right_value=intval(round( ($row->num_votes / $total_votos)*100 ) );
}
else
{
$right_value=0;
}
// dibuja el trozo de barra que le corresponde segun los votos obtenidos
$bar_length=$x+($right_value*$bar_unit);
imagefilledrectangle($im,$x,$y-2,$bar_length,$y+$bar_height,$bar_color);
//dibuja el titulo para este valor
imagettftext($im,$main_size,0,$text_indet,$y+($bar _height/2),$text_color,$font,"$row->candidate");
// dibujar contornos mostrando 100%
imagerectangle($im,$bar_length+1,$y-2,$x+(100*$bar_unit),$y+$bar_height,$line_color);
//mostrar numeros
imagettftext($im,$small_size,0,$x+(100*$bar_unit)-50,$y+($bar_height/2),$number_color,$font,
$row->num_votes."/".$total_votos);
$y=$y+($bar_height+$bar_spacing);
}
/*******************************************
Mostrar imagen
*******************************************/
Header("Content-type: image/png");
ImagePng($im);
/*******************************************
Limpiar
*******************************************/
ImageDestroy($im);
?>
una orientacion por favor y sobre todo el saber por que ocurre esto