473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

PHP-SQL interactive voting script

Hi i am trying to create an interactive PHP-Mysql voting system, so that users can input their 'favourite colour' for example in a html submit box, the php would then access the database and search the records for the input color, and if it finds it would increase the vote count by 1 in the 'votes' column for that colour, but if it doesnt find it it will add a row to the table with 'votes' value 1.
Does anyone know how to do this or where to find a tutorial?
Feb 17 '07 #1
6 2250
ronverdonk
4,258 Expert 4TB
I wonder if there is any tutorial on this. It is not that difficult to do.

It is a plain php-mysql solution and it cannot be hard to develop for a php programmer. Since you have already laid out how it should work and you also stated that you are already trying to create this solution, start coding it.

When you have problems while developing we will help you.

Ronald :cool:
Feb 17 '07 #2
Hi thanks for replying, i managed to find a registration script, which checks or updates the database and have adapted it but there are still a few mistakes in it that i cant iron out. Can you help?
[php]
<html>
<head>
<title>Untitled</title>
</head>

<body>
<?php
include 'll/connect.php';
include 'll/database.php';

if (isset($_POST['submit'])) {


if (!get_magic_quotes_gpc()) {
$_POST['colour'] = addslashes($_POST['colour']);
}
$usercheck = $_POST['colour'];
$check = mysql_query("SELECT colour FROM favourites WHERE colour = '$usercheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the colour exists add vote
if ($check2 != 0)
mysql_query("UPDATE favourites SET votes = 'votes+1'")
{
echo('your vote is counted!!');
}

// if it does not exist, add new row and 1 vote
$insert = "INSERT INTO favourites (colour, votes)
VALUES ('".$_POST['name']."', '1')";
$add_member = mysql_query($insert);
?>

<?php
}
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"

method="post">
<table border="0">
<tr><td>name:</td><td>
<input type="text" name="colour" maxlength="60">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit"

value="vote!"></th></tr> </table>
</form>

<?php
}
?>

</body>
</html>[/php]
Feb 17 '07 #3
ronverdonk
4,258 Expert 4TB
Please read the Posting Guidelines before you post in this forum!
Especially the part about enclosing code within tags!

moderator
Feb 17 '07 #4
ronverdonk
4,258 Expert 4TB
i managed to find a registration script, which checks or updates the database and have adapted it but there are still a few mistakes in it that i cant iron out
So.... what are these mistakes?

Ronald :cool:
Feb 17 '07 #5
Atli
5,058 Expert 4TB
Hi.

As Ronald says, it helps to have the error details.

I did however notice a couple of things in this part

[PHP]
//if the colour exists add vote
if ($check2 != 0)
mysql_query("UPDATE favourites SET votes = 'votes+1'")
{
echo('your vote is counted!!');
}

// if it does not exist, add new row and 1 vote
$insert = "INSERT INTO favourites (colour, votes)
VALUES ('".$_POST['name']."', '1')";
$add_member = mysql_query($insert);
?>
[/PHP]

First, when you insert or update a number in MySQL, you should leave out the ' chars:
[php]
$sql = "update mytable set num = 'num + 1'";
// should be
$sql = "update mytable set num = num + 1";
[/php]

Second, you didnt finish your update query. It needs to know what rows to update. As it is, it adds 1 to all the rows in you table.
[php]
mysql_query("UPDATE favourites SET votes = 'votes+1'")
// should be
mysql_query("UPDATE favourites SET votes = votes+1 WHERE colour = '". $_POST['name'] ."'");
[/php]


Lastly, you seeme have to left out the else there. I mean as it is, it will insert a new row for the color everytime you run the code, even if it is already in there.

This is how I see it atm
[php]
if(/*color exists */)
{
// Add 1 to the count
}

// Add the new color
[/php]

This is how I think it should be
[php]
if(/*color exists */)
{
// Add 1 to the count
}
else // <---
{
// Add the new color
}

[/php]
Feb 18 '07 #6
Thats brilliant, thanks, the script works now.
Feb 19 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: the wonderer | last post by:
This is an elementary question, but I've not been able to find the answer, so here goes: I am developing a site using php. I have the html header information in a file that I include in all the...
5
by: Ad Astra | last post by:
I am completely new to PHP and am finding installation the most difficult part to understand and implement. Could someone please review the rather long missive below and assist me? I have...
14
by: saayan | last post by:
Hi, I am using PHP 5.0.1 with Apache 2 on Win XP (SP2). My index.php file has require_once contents.php and also for functions.php. My contents.php file also has a require_once for...
1
by: jdurden | last post by:
am currently woking on building this site: www.maverick-spirit.com. everything seems to be fine except the press releases page. When you click on one of the press releases in the left column under...
0
by: tsivaraman | last post by:
I am trying to build php-5.2.1 in RedHat Linux 9. I have installed libxml2-2.6.11,mysql-5.0.33,httpd-2.2.4(apache) successfully.When i do 'make' from the php directory,i get the following...
0
by: Yannick | last post by:
Hi, I'm Julien from France, We have recently install a new Web Server for my company The server is composed : - Linux RedHat RHEL4 U4 - Httpd-2.0.52-27.ent - Oracle Database 10.2.0.1
0
by: Benjamin Grieshaber | last post by:
Hi, I´m on SuSE 9.3 with xmlrpc-c and xmlrpc-c-devel installed (ver. 0.9.10) I tried to compile php with xmlrpc support and got the following errors: ...
9
by: mekalai82 | last post by:
i have information.php file that file contain following coding <?php echo phpinfo(); ?> while i calling the URL ("http://localhost/information.php"). i am getting the coding <?php echo...
4
by: mechphisto | last post by:
I'm working on a friend's box, Fedora Core 6. It has PHP 5.1.6. I need to install mcrypt into it, and the only way I can find to do it is from source then recompile PHP. So I did all that, and got...
5
Chrisjc
by: Chrisjc | last post by:
Good afternoon, I am seeking some php configuration help. Here is the run down I am running Windows server 2003 and IIS V6.0 I have never had issues before until now. I have Symantec Antivirus 11.0...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.