RE: Online Polls

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hey Nik

Try this little piece of code (I've put in a few tips to help you with
your website coding):

========================================================================
=====================================
// config.inc.php
// here you can put website config options
// such as database config details
// this file, as well as the dbconnect.inc.php file, will be "included"
in the rest of the scripts
// by doing it this way, if any of your database connection details
change, then you only have to 
// change them in one place, the config.inc.php file, instead of
changing every single one of your scripts!

<?php

 $db_host = 'localhost';
 $db_user = 'freaknik';
 $db_password = '';
 $db = 'survey';

?>

// end config.inc.php
========================================================================
=====================================


// dbconnect.inc.php
// this script will connect to the db

<?

 $db_srv_conn = mysql_pconnect($dbhost,$db_user,$db_password)
      or die("Could not connect to the server");

 $db_conn = mysql_select_db($db,$db_srv_conn)
              or die("Could not connect to the database");

?>

// end dbconnect.inc.php
========================================================================
=====================================

// vote.php
// this is the form used for voting
<?php

 include 'config.inc.php';
 include 'dbconnect.inc.php';

 $getpollquery = "SELECT * FROM poll ORDER BY date DESC"; // THIS WILL
GET THE LATEST POLL FROM THE DATABASE
 $getpollquery_handle = mysql_query($getpollquery);
 $getpollquery_result = mysql_fetch_row($getpollquery_handle)

?>

<form method="POST" action="regvote.php">
  <p>
  <?php echo $getpollquery_result[1]; ?></p>
  <p><input type="radio" name="R1" value="response1" checked><?php echo
$getpollquery_result[2]; ?></p>
  <p><input type="radio" name="R1" value="response2"><?php echo
$getpollquery_result[3]; ?></p>
  <p><input type="radio" name="R1" value="response3"><?php echo
$getpollquery_result[4]; ?></p>
     <input type="hidden" name="poll_id" value="<?php echo
$getpollquery_result[0]; ?>">
  <p><input type="submit" value="Submit" name="submit"></p>
</form>

// end vote.php
========================================================================
=====================================

// regvote.php
// this is to process the form and insert data into the database

<?php

 include 'config.inc.php';
 include 'dbconnect.inc.php';

if (array_key_exists("R1", $_POST)) {

	switch ($_POST['R1']) { 		// check which option
was selected and create sql query
		case "response1": 

			$updatequery = "UPDATE poll SET votes1 WHERE
id=".$_POST['poll_id'];

		case "response2": 

			$updatequery = "UPDATE poll SET votes2 WHERE
id=".$_POST['poll_id'];

		case "response3": 

			$updatequery = "UPDATE poll SET votes3 WHERE
id=".$_POST['poll_id'];

	} 

}

if (mysql_query($updatequery)) {

	print "Thank-you, your vote was added successfully.";

}
else {

	print "Sorry, could not add your vote.";

}
?>

// end regvote.php
========================================================================
=====================================

Hope it works ... didn't have time to test it

Good Luck :)
 - Robin



-----Original Message-----
From: Nik [mailto:nik@guyanachronicle.com] 
Sent: 23 November 2003 11:19 PM
To: php-windows@lists.php.net
Subject:  Online Polls

OK! now I am attempting to develop a small online polling system. I set
up
my tables and html pages. However, I don't seem to understand how to
update
the information in the polling system. I know how to update using sql
but
something about this polling system I don't understand. This is my code
below.
My Table Structure
CREATE TABLE poll (

   id int(10) unsigned NOT NULL auto_increment,  //id of the question

   question varchar(255) NOT NULL,  //question

   response1 varchar(255) NOT NULL, //the first possible answer

   response2 varchar(255) NOT NULL, //the second possible answer

   response3 varchar(255) NOT NULL, //the third possible answer

   votes1 int(10) unsigned DEFAULT '0' NOT NULL, //   It is these three
I am having a problem

   votes2 int(10) unsigned DEFAULT '0' NOT NULL, //   putting the data
into when the user clicks the form

   votes3 int(10) unsigned DEFAULT '0' NOT NULL, //  to post their
choice.

   date date DEFAULT '0000-00-00' NOT NULL, //date of the poll

   PRIMARY KEY (id)

);


*********** start of code ****************

$db_host = 'localhost';
 $db_user = 'freaknik';
 $db_password = '';
 $db = 'survey';

 $db_srv_conn = mysql_pconnect($dbhost,$db_user,$db_password)
      or die("Could not connect to the server");

 $db_conn = mysql_select_db($db,$db_srv_conn)
              or die("Could not connect to the database");

 //Vote.php

 $submit = $_POST['submit'];
 $response = $_POST['response'];
 if (!$submit)
  {
   echo "kindly click submit";

  }
 else
  {
   echo "u're gett there nik<br>";
   $query = mysql_query("UPDATE survey SET vote=$vote+1 WHERE
response=$response"); //I don't quite understand how to get the correct
response to go into the correct field.

   if (!$query)
    {
     echo "Unable to cast vote";
    }
   else
    {
     echo "Thank you for voting";
    }


  }


?>

*********** end of code *****************
Thanks in advance
Nik

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux