Little poll or smth ...
vote.htm - 1th file
<html> <head> <title>Pollong</title> <head> <body> <h1>Pop Poll</h1> <p>Who will you vote for in the election?</p> <form method=post action="show_poll.php"> <input type=radio name=vote value="John Smith">John Smith<br> <input type=radio name=vote value="Mary Jones">Mary Jones<br> <input type=radio name=vote value="Fred Bloggs">Fred Bloggs<br><br> <input type=submit value="Show results"> </form> </body> <html>
show_poll.php - 2th file
<?
$mysql_db = "poll"; $mysql_user = "poll"; $mysql_pass = ""; $mysql_link = mysql_connect("localhost", "$mysql_user", "$mysql_pass");
if (!$db_conn = mysql_connect("localhost", "$mysql_user", "$mysql_pass")) { echo "Could not connect to 1 db<br>"; exit; }; mysql_select_db("$mysql_link, $mysql_db"); if (!empty($vote)) { $vote = addslashes ($vote); $query = "update poll_results set num_votes = num_votes + 1 where candidate = '$vote'"; if(!($result = mysql_query($query, $db_conn))) { echo "Could not connect to db2 <br>"; exit; } }; $query = "select * from poll_results"; if (!($result = mysql_query($query, $db_conn))) { echo "Could not connect to db3 <br>"; exit; } $num_candidates = mysql_num_rows($result); $total_votes=0; while ($row = mysql_fetch_object ($result)) { $total_votes += $row->num_votes; } mysql_data_seek($result, 0);
$width=500; $left_margin = 50; $right_margin = 50; $bar_height = 40; $bar_spacing = $bar_height/2; $font = "arial.ttf"; $title_size = 16; $main_size = 12; $small_size= 10; $text_indent = 10; $x = $left_margin + 60; $y = 50; $bar_unit = ($width=($x+$right_margin)) / 100; $height = $num_candidates * ($bar_height + $bar_spacing) + 50;
$im = imagecreate($width,$height); $while=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;
ImageFilledRectangle($im,0,0,$width,$height,$bg_color);
ImageRectangle($im,0,0,$width-1,$height-1,$line_color);
$title = "Poll Results"; $title_dimensions = ImageTTFBBox($title_size, 0, $font, $title); $title_length = $title_dimensions[2] - $title_dimensions[1]; $title_height = abs($title_dimensions[7] - $title_dimensions[1]); $title_above_line = abs($title_dimensions[7]); $title_x = ($width-$title_length)/2; $title_y = ($y - $title_height)/2 + $title_above_line;
ImageTTFText($im, $title_size, 0, $title_x, $title_y, $text_color, $font, $title);
ImageLine($im, $x, $y-5, $x, $height-15, $line_color);
while ($row = mysql_fetch_object ($result)) { if ($total_votes > 0) $percent = intval(round(($row->num_votes/$total_votes)*100)); else $percent = 0;
ImageTTFText($im,$main_size, 0, $width-30, $y+($bar_height/2), $percent_color, $font, $percent."%");
if ($total_votes > 0) $right_value = ntval(round(($row->num_votes/$total_votes)*100)); else $right_value = 0; $bar_length = $x + ($right_value * $bar_unit);
ImageFilledRectangle($im, $x, $y-2, $bar_length, $y+$bar_height, $bar_color);
ImageTTFText($im, $main_size, 0, $text_indent, $y+ ($bar_height/2), $text_color, $font, "$row->candidate");
ImageRectangle($im, $bar_length+1, $y-2, ($x+(100*$bar_unit)), $y+$bar_height, $line_color);
ImageTTFText($im, $small_size, 0, $x+ (100*$bar_unit)-50, $y+($bar_height/2), $number_color, $font, $row->num_votes."/".$total_votes);
$y=$y+(bar_height+$bar_spacing); } Header("Content-type: image/png"); ImagePng($im);
ImageDestroy ($im); ?>
tell me plz, why code is not running good.
the error is Could not connect to db |