Jump to content

Help, how.


Toy

Recommended Posts

When the button is clicked it adds +1 to the MYSQL column, please help me, i'm a beginner and i've googled all day.

 

:-\

 

EDIT: And i'd appreciate if there was some small security in this so the value isn't able to be changed with tampers data etc.

 

:P

Link to comment
Share on other sites

there really is not enough data

 

:wtf:

 

What do you mean by that?  :'(

 

What I wan't to do: When a button is pressed, PHP will add +1 (one) to a specific column in my MYSQL database, I also want to make it so your only able to click this button ONCE and the value (+1) is not able to be tampered with.

 

I'm sorry if you don't understand, I just want help, i'm a newbie at PHP&MYSQL I would be very greatful if someone helped me out!

Link to comment
Share on other sites

Of course it is simple.

 

The problem lies in that no one could possibly offer you a solution, being that they have no idea how your database is structured.  Since this is purely a database driven problem, sufficient data, such as table name and column names is important.  Any other way will just leave you flustered.

 

But, since your are so anxious for an answer.

 

UPDATE `table` SET `column` = column + 1 WHERE `id` = '$id'

 

Couple that with:

setcookie('updated','set',84000);

 

And you have an updated column, that can only be updated once per user.  IF the code is put together correctly.

Link to comment
Share on other sites

setcookie('updated','set',84000);

 

And you have an updated column, that can only be updated once per user.  IF the code is put together correctly.

 

Unless they remove their cookie each time they press it, then they can keep pressing it as long as they could possibly endure. Cookie's can be removed, IP's can be changed, service's like 10-minute mail make it possible to fool e-mail activation. Let them register to be able to press the button and ask for something that is unique to the person and can not be guessed/changed so easily like a SSN in USA so they can't have multi-accounts.

Link to comment
Share on other sites

Auch, I still haven't got it to work, I just started with php and mysql.

 

Okay, okay, here is what i'm trying to do: http://gabreli.us/.

I've got EVERYTHING to work BUT the voting function, what this website does is randomizes "statements" on the front page so I want you to be able to vote on these but only one time for each "statement".

 

What I need is pretty much just a explained code for PRESSING A BUTTON and then it will +1 or -1 with IP detection if you've voted on this statement before, that would be pretty much awesome.

Thanks in advance if someone is able to help me. :D

Link to comment
Share on other sites

Create your table that will hold your statements and a table that holds your users, create an intermediary table that will hold votes for a user on a statement and make them the primary key so that they can not vote twice.

 

users (id (PK), ..)

statements (id (PK), ..)

votes (statement_id (PK), user_id (PK))

Link to comment
Share on other sites

<div class="yes">
<form action="<?php echo $PHP_SELF;?>" method="post">
<input type="submit" name="yes_submit" value="YES, INDEED I AGREE WITH YOU!">
</form>
</div>

<div class="box">
<?php
$con = mysql_connect("xxx", "xxx", "xxx") or die (mysql_error ());
mysql_select_db ('xxx', $con) or die (mysql_error ());
$sql = mysql_query ("SELECT * FROM `statements` ORDER BY RAND() LIMIT 0,1") or die (mysql_error ());
$res = mysql_fetch_assoc ($sql);
$randomstatement = $res['statement'];
echo '<h1><span style="color: #83e079">(' . $res['positive'] . ')</span> ' . $res['statement'] . ' <span style="color: #e07979">(' . $res['negative'] . ')</span></h1>' . $res['name'] . '';
if(isset($_POST['yes_submit'])) {
$negative = "UPDATE statements SET positive = positive + 1";
}
if(isset($_POST['no_submit'])) {
$negative = "UPDATE statements SET negative = negative + 1";
}

?>
</div>

<div class="no">
<form action="<?php echo $PHP_SELF;?>" method="post">
<input type="submit" name="no_submit" value="NO, I DO NOT AGREE WITH THIS STATEMENT!">
</form>
</div>

 

picdv.png

 

DB structure, table: statements with 4 columns: statement, name, positive, negative.

 

That is how i've tried to do it now but it just wont work, can anyone help me? Put in mind that i'm a complete beginner at php&mysql so the most of the code is from tutorials and examples i've followed.

Link to comment
Share on other sites

Remove positive, negative from your statements table. Create a users table and create the intermediary table votes which has the fields: statement_id, user_id, positive_negative. Insert 1 for a positive vote, -1 for a negative vote.

 

Okay, but the problem is that i'm still not able to input +1 or -1 could you please help me, what am I doing wrong in the code?

 

And i'd rather not create a new table as I don't want to complex it more than it already is.

THE ONLY THING I WANT IS A SCRIPT FOR:

 

When I press a button it will add one point to the column and you're only able to vote once per IP.

 

That's everything I want, is it really that hard? :(

Link to comment
Share on other sites

THE ONLY THING I WANT IS A SCRIPT FOR:

 

When I press a button it will add one point to the column and you're only able to vote once per IP.

 

That's everything I want, is it really that hard? :(

 

Yes, if you want a voting system that isn't easy to cheat then it is quite complex (see ignace's posts above). 

 

If you just want a basic voting system without creating any new tables to log IP addresses etc. then you may be able to set a cookie when the voting page loads and only allow people with this cookie set to vote (then update the cookie to record if they've voted).  This way a user will need to both accept and then delete the cookie (as opposed to just blocking cookies outright) between every vote if they want to spam votes.  It's entirely possible, but the question is... How much do you care?

 

As for the database query, jcbones already had you covered there in his post.

 

Basically this isn't as easy as you're hoping it will be.  Hope this clears things up a bit though.

Link to comment
Share on other sites

THE ONLY THING I WANT IS A SCRIPT FOR:

 

When I press a button it will add one point to the column and you're only able to vote once per IP.

 

That's everything I want, is it really that hard? :(

 

Yes, if you want a voting system that isn't easy to cheat then it is quite complex (see ignace's posts above). 

 

If you just want a basic voting system without creating any new tables to log IP addresses etc. then you may be able to set a cookie when the voting page loads and only allow people with this cookie set to vote (then update the cookie to record if they've voted).  This way a user will need to both accept and then delete the cookie (as opposed to just blocking cookies outright) between every vote if they want to spam votes.  It's entirely possible, but the question is... How much do you care?

 

As for the database query, jcbones already had you covered there in his post.

 

Basically this isn't as easy as you're hoping it will be.  Hope this clears things up a bit though.

 

Yes he provided some code but I couldn't get it to work could you please be an angel and check my code and see whats wrong?

Link to comment
Share on other sites

The main problem I can see with your code is:

 

if(isset($_POST['yes_submit'])) {
$negative = "UPDATE statements SET positive = positive + 1";
}
if(isset($_POST['no_submit'])) {
$negative = "UPDATE statements SET negative = negative + 1";
}

Now what? You've built the SQL statement.  You need to execute it:

 

$result = mysql_query ($negative);
if (mysql_affected_rows() < 1) die("No update was made");

Also, I don't see any primary key as such in your statements table.  When you say:

 

$negative = "UPDATE statements SET negative = negative + 1";

MySQL won't know which specific statement to update the vote count for.  You want a WHERE clause like:

 

$query = "UPDATE `statements` SET positive = positive + 1 WHERE statement_id = 1";

Otherwise, I think MySQL will update the positive vote count in all rows.

 

This should get you going.  Then you can start thinking about deterring spammers.

Link to comment
Share on other sites

You could put the ID of the statement in a hidden field in the form.  This way it is also posted with the user's positive or negative vote.

 

<input type="hidden" name="statement_id" value="<?php echo $res['ID']; ?>" />

 

Just remember that the user can still edit the ID in the hidden field if they know what they are doing - so you will need to sanitize/validate it before using it in a database query.

Link to comment
Share on other sites

You could put the ID of the statement in a hidden field in the form.  This way it is also posted with the user's positive or negative vote.

 

<input type="hidden" name="statement_id" value="<?php echo $res['ID']; ?>" />

 

Just remember that the user can still edit the ID in the hidden field if they know what they are doing - so you will need to sanitize/validate it before using it in a database query.

 

Hm.. I don't really get everything you're saying, please keep in mind that i'm only a beginner.

 

But I tried something like this:

 

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

$negative = "UPDATE statements SET negative = negative + 1 WHERE ID = ['statement_id']";

$result = mysql_query ($negative);

}

 

and in the end of my file:

 

<input type="hidden" name="statement_id" value="<?php echo $res['ID']; ?>">

 

But it did not seem to work, am I doing something wrong?

Link to comment
Share on other sites

If you followed any sort of tutorial when creating your "statements" table in MySQL, you would probably have created an "id" or "statement_id" column that auto increments when a new statement is entered into the database.

 

If this is the case, when you query the database for a statement for the user to vote on, you can also grab that statement's id - along with the username, positive and negative votes etc.

 

Then when you build the voting form, you include that id hidden in the form as I showed you:

 

<form method="post" action="vote.php">
<input type="hidden" name="statement_id" value="<?php echo $statement_id_from_database; ?>" />
<input type="submit" name="no_submit" value="Disagree" />
<input type="submit" name="yes_submit" value="Agree" />
</form>

 

Once the form has been posted, you can access the statement id like this:

 

$statement_id = (int) $_POST['statement_id']; // (int) insists that the value be an integer
$negative = "UPDATE statements SET negative = negative + 1 WHERE ID = $statement_id";
// etc.

This sort of thing is generally covered by most tutorials so you may want to follow a few of those to get a better understanding.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.