|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
hey folks,
i'm brand new to PHP and mySQL and have run into a bit of difficulty. i have been trying to do the web poll tutorial on devshed and all was going well until i upgraded my apache server to accept php4. i did this so that the dynamic graphic creation in the tutorial would work. since then the graphics have worked fine but the poll has not worked properly at all. when you hit submitall i get is an error try again message and no changes to the database are made. i have run into similar problems with another tutorial and a ready made script- i cannot get the dotcomments script for allowing comments on weblogs to work. it hangs in a similar way; when you hit submit to send your comment, the page simply refreshes and does nothing else. also, i followed the webmonkey tutorial on adding to and querying a simple database through PHP and everything worked fine until the point of adding new records to the database via a form and submit. once again same problem- page refreshes, but no update of the database. i know php is working because i've managed to install a random image selector on my page which works fine. also all the other scripts work properly until any data has to be submitted by a form. then they fail. the poll code is listed below start.php <? <html> <head> <basefont face="Arial"> </head> <body bgcolor="white"> <? // start.php - displays poll and responses // includes include("config.php"); include("common.php"); // connect to database and query $connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect!"); $query = "SELECT id, question, response1, response2, response3 from $table ORDER BY id DESC LIMIT 0,1"; $result = mysql_db_query($database, $query, $connection) or die ("Could not execute query: $query. " . mysql_error()); // if questions are available, display vote form if (mysql_num_rows($result) > 0) { list ($id, $question, $response1, $response2, $response3) = mysql_fetch_row($result); ?> <form method="post" action="vote.php"> <b><? echo $question; ?></b> <p> <input type="Radio" name="response" value="1"><? echo $response1; ?> <p> <input type="Radio" name="response" value="2"><? echo $response2; ?> <p> <input type="Radio" name="response" value="3"><? echo $response3; ?> <input type="hidden" name="id" value="<? echo $id; ?>"> <p> <font size=-2><a href="archive.php?id=<? echo $id; ?>">view results</a></font> <font size=-2><a href="archive.php">view past polls</a></font> <p> <input type=submit name=submit value="Vote"> </form> <? } // or display a status message else { ?> <i>No polls available!</i> <? } // close connection mysql_close($connection); ?> </body> </html> ___________________________________________________________________________ vote.php <? // vote.php - record vote // check to ensure that the form has been submitted if (!$submit || !$response) { // rest of error message code } // check the cookie to ensure that user has not voted already else if ($lastpoll && $lastpoll == $id) { ?> <html> <head> <basefont face="Arial"> </head> <body bgcolor="white"> <i>You have already voted once. Come back in a few days for another poll, or <a href=archive.php>click here</a> to view previous polls</i> <? } // all is well - refresh the cookie (or set a new one) and process the vote else { setCookie("lastpoll", $id, time()+2592000); // rest of vote processing code } ?> ____________________________________________________________________________________ config.php <? // config.php - global variables for all database operations $hostname="localhost"; $user="admin"; $pass="K89BhQ"; $database="poll"; $table="poll"; ?> __________________________________________________________________ poll database looks like CREATE TABLE poll ( id int(10) unsigned NOT NULL auto_increment, question varchar(255) NOT NULL, response1 varchar(255) NOT NULL, response2 varchar(255) NOT NULL, response3 varchar(255) NOT NULL, votes1 int(10) unsigned DEFAULT '0' NOT NULL, votes2 int(10) unsigned DEFAULT '0' NOT NULL, votes3 int(10) unsigned DEFAULT '0' NOT NULL, date date DEFAULT '0000-00-00' NOT NULL, PRIMARY KEY (id) ); _____________________________________________________________________ the database is prepopulated with the appropriate questions, the poll date and 0s for the votes. there are other files involved, however it is between start and vote that the thing fails. all i ever get is the error, try again message. and the database never changes. if i update the database from the server, the changes do show up properly on the results page. the poll as i have it set up can be seen at - URL i hope someone can help me as i am at a total loss. regards, ian watson |
|
#2
|
|||
|
|||
|
It looks like you aren't connecting to the database.
This line: $connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect!"); loads the command into a variable but the variable is never used. If you did mysql_connect($hostname, $user, $pass) or die ("Unable to connect!"); This would connect to the database. This also goes for $query and $result. Also I see no insert commands in your queries to insert data into your database. E. |
![]() |
| Viewing: Dev Shed Forums > Other > Beginner Programming > can't get php to write to mysql |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|