By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

PHP-SQL interactive voting script

P: 6
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
Share this Question
Share on Google+
6 Replies


ronverdonk
Expert 2.5K+
P: 4,258
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

P: 6
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
Expert 2.5K+
P: 4,258
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
Expert 2.5K+
P: 4,258
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
Expert 5K+
P: 5,058
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

P: 6
Thats brilliant, thanks, the script works now.
Feb 19 '07 #7

Post your reply

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