Guygreen Posted December 10, 2010 Share Posted December 10, 2010 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> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 10, 2010 Share Posted December 10, 2010 is anything happening at all? or is the page blank. The first thing that jumps out at me is mysql_db_query(), which is deprecated. Quote Link to comment Share on other sites More sharing options...
Guygreen Posted December 10, 2010 Author Share Posted December 10, 2010 This is what I see. When I enter a name and hit Submit, nothing happens. No records are created in the database either. See attached. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted December 10, 2010 Share Posted December 10, 2010 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+ Quote Link to comment Share on other sites More sharing options...
Guygreen Posted December 11, 2010 Author Share Posted December 11, 2010 Thanks BlueSkyIS! It kindda worked. I'll start a fire and get PHP 5+ Quote Link to comment Share on other sites More sharing options...
Guygreen Posted December 11, 2010 Author Share Posted December 11, 2010 Any suggestions for a PHP 5+ book? I am an ex-developer that is coming back to programming... Thanks, Guy Quote Link to comment Share on other sites More sharing options...
Jerred121 Posted December 11, 2010 Share Posted December 11, 2010 I don't know if a book is really necessary, there are plenty of excellent resources on the web to get you going. Google is your best friend. Quote Link to comment Share on other sites More sharing options...
Guygreen Posted December 11, 2010 Author Share Posted December 11, 2010 Thanks! Have a great day. Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 11, 2010 Share Posted December 11, 2010 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. Quote Link to comment Share on other sites More sharing options...
Guygreen Posted December 11, 2010 Author Share Posted December 11, 2010 Good points. Thanks. Guy Quote Link to comment Share on other sites More sharing options...
Guygreen Posted December 11, 2010 Author Share Posted December 11, 2010 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> Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 11, 2010 Share Posted December 11, 2010 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. Quote Link to comment Share on other sites More sharing options...
Guygreen Posted December 11, 2010 Author Share Posted December 11, 2010 I'll definitely use this book to start a fire in my fireplace. Guy Ottawa, Ontario, Canada Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.