Make each user vote only once with Session username - php

I made a simple vote poll it works fine,but i can't make it for each user i mean i can somehow hide if i even manage votepoll to be saved in users info but it's impossible when i tried insert into users it makes a new user with only votePoll declared...everything else is empty,i think it's a wrong way to approach,i can't even mind how to show votepoll whitch user is voteing.
This is my code of vote
<?php
if(isset($_POST['submitme'])){
$submitme=$_POST['submitme'];
$yesOrNo=$_POST['yesOrNo'];
if(!empty($yesOrNo)){
$sqlv=$con->query("INSERT INTO votepoll (yesorno) VALUES ('{$yesOrNo}')");
header('location:index.php');
}
}
?>

Add your "yesorno" row in users,than make query update,don't insert!
if(isset($_POST['submitme'])){
$submitme=$_POST['submitme'];
$yesOrNo=$_POST['yesOrNo'];
if(!empty($yesOrNo)){
$sqlv=mysqli_query($con,"UPDATE users SET yesorno='$yesOrNo' WHERE username='$username'");
}
}
okay so when you click button yesorno is updated in rows as "yes" or "no" ,you should know how user statuses work, if($userStatus==1) and 1 is equal to Admin do so and etc. i'm just giving you direction now just get yesrno from rows and set it equal to variable and if("$userVote!="yes" && $userVote!="no"):
your html cde below and endif;
I just gave you some idea how to get through this problem and gave a direction.

Related

PHP MySQL update not execute if call twice in short time

I'm using CodeIgniter with MySQL. The case is, customer do shop, and pay with account.
Code as follow:
$globallockname = 'MEM_LOCK_ORDER_PAYLOCK';
//lock with memcache add
if (!$this->getGlobalLock($globallockname, 10)){
dexit(array('error'=>true,'msg'=>'get loack failed'));
}
//check if account is enough
$member = $this->admin_model->getItem('member', array('id'=>$this->_member['id']), false);
if ($this->_member['account'] >= $amountpay){]
//pay
$this->admin_model->update('member', "`account` = `account` - ".$amountpay, "`id` = ".$this->_member['id']);
}else{
//unlock
$this->delGlobalLock($globallockname);
dexit(array('error'=>true,'msg'=>'account is short, please recharge'));
}
//unlock
$this->delGlobalLock($globallockname);
When a customer clicks "buy" once, everything goes fine, but if they click "buy" twice in a short time, this script will execute twice, and the first update will not work.
I've checked the return of $db->query, each of them return true.
You are updating the same field twice. So you will get the last update only.
ie the first update is overwritten with second update..
hope this helps...
once buy button is clicked, disable it and show with progressing caption. This will avoid further clicks.
I did a stupid thing, there's nothing wrong with mysql or codeigniter. In my index controller, I go member member info and then set it again, so the memcache lock did not work, the information was overwrite...

Get user information from mysql database

I'm trying to do a very easy user management.
I want to select and display the information from the mysql database.
I have connected to the database and it works fine. And I have a log in form and registration which works fine. Now when the user is logged in I want to display the information and the user should be able to update it.
Can i just use a simple thing like this:
<?php
$firstname= $_SESSION['user_name'];
$email= $_SESSION['user_email'];
if ($email!= null) {
echo $email;
}
else {
echo 'no email found';
}
?>
You are leaving a lot out, so it is hard to know what all you want to do, but you can use the UPDATE statement to change the database values which have already been inserted.
UPDATE users SET user_email = 'some_email'
WHERE user_name = 'some_user';
Other than that I'm not sure what you're asking for. The whole HTML form?

Validate user's rights

I'm writing an hour registration system for my projectteam at school. Everything is pretty much working, but I can't seem to get the validation of user rights to work.
Validation is done in the acctype field within the user table. If 0 (guest), you can only view the list of hours, if 1 (specialist) you can add your own hours and if 2 (project-manager), you can review the hours users have submitted.
At first I was only using the $account query but instead of selecting them all I selected acctype only.
Does anyone have any idea what am I doing wrong?
$cookie = $_COOKIE['user'];
$account = mysqli_query($conn, "SELECT * FROM user WHERE user = '" . $cookie . "'");
$acctype = mysqli_fetch_assoc($account->acctype);
if(isset($cookie) && $acctype >= 1) {
} else {
}
Jonathan
I believe there's a few things wrong here:
You're reading the cookie before checking if it's set. That's a mistake. You should see if there's a cookie, and THEN read it in. You also don't need to assign it a separate variable.
Note: As I said in my comment, user data should be in a session, not a cookie.
I don't know what your DB schema looks like, but your query is SELECT * FROM user, meaning that if you have an ID, a user name, an access level, and some other things, you're going to get ALL that into the var $acctype, which obviously isn't an integer.
I think the fix is to execute your query, get your results, and then compare the row(s) returned and only check the acctype part:
if ($row['acctype'] >= 1){
}
Documentation: http://us1.php.net/mysqli_fetch_assoc

Display data if usertype is admin otherwise display error message

I currently have a list of users in my mysql database. One of the columns is "type". I am trying to display certain data if type is equal to admin. If type is equal to anything else, it should just echo an error message.
Unfortunately, I have tried multiple methods but it just does not seem to be working out for me. Can anyone help me get this to work properly?
This is what I have, but obviously I am doing something wrong....
<?php
$usertype = $_SESSION['type'];
if ($usertype == "admin" ){
?>
admin stuff only goes here
<?
}
else
{
echo "not priveleged usertype";
}
?>
EDIT:
The following code works when displaying via username, however, I need content displayed by usertype, not the username.
<?php
if($_SESSION['user']['username'] == "oneoftheadminusernames" )
{
?>
Each page has to start with
<?php
#session_start();
?>
otherwise, php does not "see" the sessions contents. So that's probably it.
The # prevents the php error: A session has already been started... by the way.
Now, every page that uses the session must have this directive at the top.
At least, in a quick example, that reproduces your error perfectly.
If you are saving each logged in users type field in $_SESSION['type'] variable than the code you are writing is correct. Or if you are storing type in another variable than you that variable to check.
i have an idea like add a field EnableFlag in the table. if enablee flag is set to 1 consider it as a admin else as a User;

Update table value on page view?

I am trying to create a PHP trigger for when a user views certain pages on my website it will update the user table in the points section.
I understand the process would work something like this
on page view > update user > where user id is (**get username from session**) > add 5 to points row
Anyone have any idea how to set up something simple like this for giving users simple points for viewing pages?
My site is using PHP and mySQL for the database.
Use cookies or session variables to keep track of the user details like the username or ID. So making a pageview trigger would be as easy as adding a mysql query at the top of every page which would update the database table for views. Kinda the same way that forums operate.
E.g
<?php
session_start();
$db_connection = mysqli_connect('host','username','password','db');
$user_id = $_SESSION['userid']; //That is asssuming that you had gotten the user id on login
mysqli_query($db_connection, 'UPDATE page_views SET views_column=views_column+1 WHERE userid=$user_id');
?>
Yes, you could do something like (if you own the page the user has to visit):
<?php
$pointsForThisSite = 5;
include "points_adder.php";
?>
While Points_adder looks whether $pointsForThisSite is defined and > 0, then adds the Points to the database as you descripbed.
Is that what you are looking for?
Create a php function and call it everytime the user enter the page.
You don't need a mysql trigger because, the action is at the webpage.
function add_points($user, $page){
//If users visits too many maybe you don't want to gave him some points.
//add points
}
and invoke the function in that pages you want to score
The most unobtrusive way to do this is with an AJAX call after the page has loaded. The call should be to an include file that performs the database update operation and returns a 204 response so that the visitor's browser doesn't wait for response content.
For an Apache server;
header('HTTP/1.0 204 No Content');
header('Content-Length: 0', true);
header('Content-Type: text/html', true);
flush();
// do the table update here

Categories