commercial problem<

liunx

Guest
here is an example of book code


create table poll_results (
candidate varchar(30),
num_votes int
);

insert into poll_results values
('John Smith', 0),
('Mary Jones', 0),
('Fred Bloggs', 0)
;


the bit of code that supoosed to insert the form data...

if (!empty($vote)) // if they filled the form out, add their 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 db<br>";
exit;
}
};


it doesn't update - why? It does the rest of the code fine, shows the form, shows the poll and graph - but DOES NOT update the db - and throws no connection errors.

I don't get it and it is driving me crazy...I'm not sure exactly how mysql works as far as letting you enter operations inside your mysql statement.

Since you're not getting an error, it means the query must be valid, but if nothing gets changed, then my best guess would be that the "+ 1" isn't getting processed.

I would create a new variable that is the sum of num_votes and 1:

$newVar = $num_votes + 1;

then issue your query with $newVar in the place of num_votes.this is perfectly valid

$query = "update poll_results
set num_votes = num_votes + 1
where candidate = '$vote'";

but the question comes to this.

is this emtpy

if (!empty($vote))

if it is then it wil not process the query. where is it coming from?Thats not the entire file; thats
identical to the example in chapter #19
of PHP and MySQL Web Development, 2nd Edition.

Here's the full thing.yes, it is the example from chapter 19 - but it does not work.

yes, it does send a proper value for $vote - so it is not empty

and it still doesn't work!

I think it is the +1 as well, but the problem then becomes extracting the num_votes so that i can then increment it so this: $newVar = $num_votes + 1;
wont work either.

can someone else try this and see if it is just a 'me' thing?


<?

<html>
<head>
<title>Polling</title>
<head>
<body>
<h1>Pop Poll</h1>
<p>Who will you vote for in the election?</p>
<form method=post action="show_poll2.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>


mysql
create table poll_results (
candidate varchar(30),
num_votes int
);

insert into poll_results values
('John Smith', 0),
('Mary Jones', 0),
('Fred Bloggs', 0)
;





/*******************************************
Database query to get poll info
*******************************************/
// log in to database
if (!$db_conn = @mysql_connect("localhost", "poll", "poll"))
{
echo "Could not connect to db<br>";
exit;
};
@mysql_select_db("poll");

if (!empty($vote)) // if they filled the form out, add their 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 db<br>";
exit;
}
};

// get current results of poll, regardless of whether they voted
$query = "select * from poll_results";
if(!($result = @mysql_query($query, $db_conn)))
{
echo "Could not connect to db<br>";
exit;
}
$num_candidates = mysql_num_rows($result);

// calculate total number of votes so far
$total_votes=0;
while ($row = mysql_fetch_object ($result))
{
$total_votes += $row->num_votes;
}
mysql_data_seek($result, 0); // reset result pointer


/*******************************************
Initial calculations for graph
*******************************************/
// set up constants
$width=500; // width of image in pixels - this will fit in 640x480
$left_margin = 50; // space to leave on left of image
$right_margin= 50; // ditto right
$bar_height = 40;
$bar_spacing = $bar_height/2;
$font = "arial.ttf";
$title_size= 16; // point
$main_size= 12; // point
$small_size= 12; // point
$text_indent = 10; // position for text labels on left

// set up initial point to draw from
$x = $left_margin + 60; // place to draw baseline of the graph
$y = 50; // ditto
$bar_unit = ($width-($x+$right_margin)) / 100; // one "point" on the graph

// calculate height of graph - bars plus gaps plus some margin
$height = $num_candidates * ($bar_height + $bar_spacing) + 50;

/*******************************************
Set up base image
*******************************************/
// create a blank canvas
$im = imagecreate($width,$height);

// Allocate colors
$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;

// Create "canvas" to draw on
ImageFilledRectangle($im,0,0,$width,$height,$bg_color);

// Draw outline around canvas
ImageRectangle($im,0,0,$width-1,$height-1,$line_color);

// Add title
$title = "Poll Results";
$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; // center it in x
$title_y = ($y - $title_height)/2 + $title_above_line; // center in y gap
ImageTTFText($im, $title_size, 0, $title_x, $title_y,
$text_color, $font, $title);

// Draw a base line from a little above first bar location
// to a little below last
ImageLine($im, $x, $y-5, $x, $height-15, $line_color);

/*******************************************
Draw data into graph
*******************************************/
// Get each line of db data and draw corresponding bars
while ($row = mysql_fetch_object ($result))
{
if ($total_votes > 0)
$percent = intval(round(($row->num_votes/$total_votes)*100));
else
$percent = 0;

// display percent for this value
ImageTTFText($im, $main_size, 0, $width-30, $y+($bar_height/2),
$percent_color, $font, $percent."%");
if ($total_votes > 0)
$right_value = intval(round(($row->num_votes/$total_votes)*100));
else
$right_value = 0;

// length of bar for this value
$bar_length = $x + ($right_value * $bar_unit);

// draw bar for this value
ImageFilledRectangle($im, $x, $y-2, $bar_length, $y+$bar_height, $bar_color);

// draw title for this value
ImageTTFText($im, $main_size, 0, $text_indent, $y+($bar_height/2),
$text_color, $font, "$row->candidate");

// draw outline showing 100%
ImageRectangle($im, $bar_length+1, $y-2,
($x+(100*$bar_unit)), $y+$bar_height, $line_color);

// display numbers
ImageTTFText($im, $small_size, 0, $x+(100*$bar_unit)-50, $y+($bar_height/2),
$number_color, $font, $row->num_votes."/".$total_votes);

// move down to next bar
$y=$y+($bar_height+$bar_spacing);
}

/*******************************************
Display image
*******************************************/
Header("Content-type: image/png");
ImagePng($im);


/*******************************************
Clean up
*******************************************/
ImageDestroy($im);
?>prove to me that this is not empty

if (!empty($vote))

echo $vote right before that

echo "vote=".$vote;
if (!empty($vote))

my guess is it will be empty. do this

if (!empty($_POST['vote']))

if that still doesn't work, do this


if (!empty($_POST['vote'])) // if they filled the form out, add their vote
{
$vote = addslashes($_POST['vote']);
$query = "update poll_results
set num_votes = num_votes + 1 where candidate = '$vote'";
@mysql_query($query)) or die ("Could not connect to db". mysql_error() );
}first i started testing the heck out of it by echoing all the results from the form making sure they had indeed passed - they did the little buggers!

then, convinced it was the wrong form of query - i tested all of them (yes time consuming, and often pointless - but there you are). nope

i decided to go at it sideways and go with the primary key indetifying the 'vote' (and I renamed all the fields and variables, just to be thorough ;))

any way - this works!


create table GRAVEY (
ID int(10) unsigned NOT NULL auto_increment,
p_ID tinyint(4) NOT NULL default '0',
cand varchar(30),
votes int,
PRIMARY KEY (ID)
);

insert into GRAVEY values

(4, 1, 'yes', 1),
(5, 1, 'no', 1)
;

function ACTION_getTotal( $ID ) {
$this->votetotal = $this->dbh->scalar_query(// my predefined db handlers
"select votes from GRAVEY where ID = ?",
array( $ID )
);
if($this->votetotal >= 0){
$this->dbh->query(
"update GRAVEY set votes = votes + 1 where ID = ?",
array( $ID )
);
}
else{ echo ( 'bugger, that didnt work' ); }//check
}


thanks very much all for inspiring me with ways to beat this! - so far...
:)sideways is right, you went form beginning chapter to a more advanced chapter to do the exact samething. why did you move over to classes and stuff?
 
Back
Top