Jump to content

PHP Simple Code not working


Guygreen

Recommended Posts

I am running PHP Version 5.3.1.

The following Code does not write to my database. It is code that I took from the PHP Pocket Reference from O'Reilly but it does not work...

What's wrong with this code?

 

Thanks in advance.

Guy

 

<?php

if($vote && !$already_voted)

SetCookie('already_voted',1);

?>

<html>

<head>

<title>Name the Baby</title>

</head>

<h3>Name the Baby</h3>

<form action="baby.php" method="POST">

  <p>Suggestion:

    <input type="text" name="new_name"/>

  </p>

  <input type="submit" value="Submit idea and/or vote"/>

<?php

mysql_pconnect("localhost","root","password");

$db = "babynames";

$table = "baby_names";

 

if($new_name) {

if(!mysql_db_query($db, "insert into $table values ('$new_name',0)")) {

echo mysql_errno().': '.

mysql_error()."<br />\n";

}

}

if($vote && $already_voted) {

echo '<p><b>Hey, you voted already ';

echo "Vote ignored.</b></p>\n";

}

else if($vote) {

if(!mysql_db_query($db, "update $table set votes=votes+1 where name='$vote'")) {

echo mysql_errno().': '.

mysql_error()."<br />\n";

}

}

$result=mysql_db_query($db,"select sum(votes) as sum from $table");

if($result) {

$sum = (int) mysql_result($result,0,"sum");

mysql_free_result($result);

}

$result=mysql_db_query($db, "select * from $table order by votes DESC");

echo <<<EOD

<table border="0"><tr><th>Vote</th>

<th>Idea</th><th colspan="2">Votes</th></tr>

EOD;

while($row=mysql_fetch_row($result)) {

echo <<<FOO

<tr><td align="center">

<input type="radio" name="vote" value="$row[0]"></td>

<td>$row[0]</td>

<td align="right">$row[1]</td>

<td>

FOO;

if ($sum && (int)$row[1]) {

$per = (int)(100 * $row[1]/$sum);

echo '<img src="bline.gif" height=12 ';

echo "width=$per> $per %</td>";

}

echo "</tr>\n";

}

echo "</table>\n";

mysql_free_result($result);

?>

<input type="submit" value="Submit idea and/or vote" />

<input type="reset" />

</form>

</body></html>

 

Link to comment
Share on other sites

i think your code assumes that POST variables are global. They aren't, by default, in your PHP. You should set their values explicitly, like

 

$new_name = (isset($_POST['new_name']))?$_POST['new_name']:'';

 

if you had error_reporting(-1) at the top of your file, you'd probably get warnings about these values not being set.

 

P.S. I suggest that you use that book for something else, maybe to start a fire. Get a newer reference updated for PHP 5+

 

Link to comment
Share on other sites

I would advise you check this thread:

 

http://www.phpfreaks.com/forums/miscellaneous/good-programming-and-web-design-books/

 

One of the worst things you can do is learn based on outdated information, like you have been. *always* check the date of publication or copyright if the book has been updated since. In fact, same goes for information you read on websites. It is critical you have the information 'validated' so to speak, so don't get books or read tutorials without good ratings (check amazon).

 

Oh, another thing which helped me understand why what I was reading was bad information is by reading bad reviews. Most of the time you can tell if somebody is clued up on the subject matter and they tell you exactly why the book or tutorial contains bad information or is outdated.

 

Hope that helps.

 

 

Link to comment
Share on other sites

Got it to work... Thanks to all for the good advise. It boils down that the book's examples weren't complete...

The database contains:

  name varchar(30)

  vote int(4)

 

The working code is:

<?php
if($vote && !$already_voted)
	SetCookie('already_voted',1);
	//Global POST variables
	$new_name = (isset($_POST['new_name']))?$_POST['new_name']:'';
	$vote = (isset($_POST['vote']))?$_POST['vote']:'';
?>
<html>
<head>
<title>Name the Baby</title>
</head>
<h3>Name the Baby</h3>
<form action="baby.php" method="POST">
  <p>Suggestion:
    <input type="text" name="new_name"/>
  </p>
  <input type="submit" value="Submit idea and/or vote"/>
<?php
mysql_pconnect("localhost","root","password");
$db = "babynames";
$table = "baby_names";

if($new_name) {
if(!mysql_db_query($db, "insert into $table values ('$new_name',0)")) {
	echo 'line 23: '.mysql_errno().': '.
		mysql_error()."<br />\n";
}
}
if($vote && $already_voted) {
echo '<p><b>Hey, you voted already ';
echo "Vote ignored.</b></p>\n";
}
else if($vote) {
if(!mysql_db_query($db, "update $table set votes=votes+1 where name='$vote'")) {
	echo mysql_errno().': '.
		mysql_error()."<br />\n";
}
}
$result=mysql_db_query($db,"select sum(votes) as sum from $table");
if($result) {
$sum = (int) mysql_result($result,0,"sum");
mysql_free_result($result);
}
$result=mysql_db_query($db, "select * from $table order by votes DESC");
echo <<<EOD
<table border="0"><tr><th>Vote</th>
<th>Idea</th><th colspan="2">Votes</th></tr>
EOD;
while($row=mysql_fetch_row($result)) {
echo <<<FOO
	<tr><td align="center">
	<input type="radio" name="vote" value="$row[0]"></td>
	<td>$row[0]</td>
	<td align="right">$row[1]</td>
	<td>
FOO;
if ($sum && (int)$row[1]) {
	$per = (int)(100 * $row[1]/$sum);
	echo '<img src="bline.gif" height=12 ';
	echo "width=$per> $per %</td>";
}
echo "</tr>\n";
}
echo "</table>\n";
mysql_free_result($result);
?>
<input type="submit" value="Submit idea and/or vote" />
<input type="reset" />
</form>
</body></html>

 

Link to comment
Share on other sites

Don't forget however that mysql_db_query is deprecated as are probably more of what you've been reading in that reference book. That's without even taking into account modern day best practice. The above code you posted will not work on exclusively php 5 servers.

 

I'd also advise you look into OOP and how you can use that to produce more maintainable, reusable code.

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.