Beginner Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherBeginner Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old July 19th, 2001, 06:41 AM
dideedum@hotmai dideedum@hotmai is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2001
Posts: 1 dideedum@hotmai User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy can't get php to write to mysql

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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<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

Reply With Quote
  #2  
Old July 19th, 2001, 12:22 PM
Theeggman Theeggman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Posts: 266 Theeggman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 33 sec
Reputation Power: 8
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherBeginner Programming > can't get php to write to mysql


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway