I have a reminder mail sent to those who do not log on to my site after 30 days. Earlier I got an answer on this forum to create a seperate field and then update it as mentioned here: Need help on unix time stamp.
I have created a new field lastprelogin, now how do I update the respective field when I send the mail for inactive users.
<?php
include("include/data.php");
$query = "SELECT * FROM myusers WHERE DATE_ADD(FROM_UNIXTIME(lastprelogin), INTERVAL 15 DAY) < CURDATE()";
$result = mysql_query($query);
$num = mysql_numrows($result);
$i = 0;
while ($i < $num)
{
//send mail code
$sendmail = mail($to,$subject,$msg,$headers);
$i++;
if($sendmail) {
$query1 = "UPDATE myusers SET lastprelogin='".time()."'";
$result2 = mysql_query($query1);
}
}
?>
How can I update the respective user's lastprelogin field after sending the mail?
I am lost here beccause am unable to understand the logic in this part.
You need to loop through your results by using mysql_fetch_assoc or similar function.
Your update query needs to include the ID of the record you wish to update.
You should not use the mysql_* functions anymore as they are becoming deprecated soon. Use mysqli instead
<?php
include("include/data.php");
$query = "SELECT * FROM myusers WHERE DATE_ADD(FROM_UNIXTIME(lastprelogin), INTERVAL 15 DAY) < CURDATE()";
$result = mysql_query($query);
while ($user = mysql_fetch_assoc($result))
{
//send mail code
$sendmail = mail($user['email_address'],$subject,$msg,$headers);
$i++;
if($sendmail){
$query1 = "update myusers set lastprelogin='".time()."' WHERE id = " . $user['id'];
$result2 = mysql_query($query1);
}
}
?>
The logic of Your script is simple:
retrieve all the users that didn't log in for last 15 days
send an email to each user
and if that succeeds also update the field lastprelogin for that user
You have some important errors within Your script and this should be like this:
include("include/data.php");
$query = "SELECT * FROM myusers WHERE DATE_ADD(FROM_UNIXTIME(lastprelogin), INTERVAL 15 DAY) < CURDATE()";
$result = mysql_query($query);
while($user = mysql_fetch_assoc($result)) {
// assuming that myusers table has these columns: user_id, user_name, user_email, lastprelogin
//send mail code
if(mail($user['user_email'],'Please log in','Please login to my site',$headers)) {
$query1 = "update myusers set lastprelogin='".time()."' where user_id = {$user['usri_id']}";
$result2 = mysql_query($query1);
}
}
// end.
As a $headers variable You can set a From header, etc. Look for PHP mail function here: http://php.net/mail
Also the right query for updating should be this one:
"update myusers set lastprelogin='".time()."' where user_id = {$user['user_id']}"
anyway You will update the lastprelogin of all users everytime...
You will need to get some id from your myuser table and run the update query with where id = $id.
Related
I'm developing an api that can detect simultaneous request (For example 5 request is supported).
I have a sql table with rows limit and requests_made.
I have my attempt here the sleep(30) is just a delay for example.
<?php
include_once('connectdb.php');
$api_key_url = $_GET['api_key'];
$sql = "SELECT * FROM table WHERE api_key = '$api_key_url'";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($query)){
$account1 = $row['email'];
$requests_made= $row['requests_made'];
$limit = $row['limit'];
}
if ($check_speed > $check_limit){
echo"Your request couldn't be served. Your account is already running at maximum requests";
exit();
}
mysqli_query($conn, "UPDATE hash SET check_speed = requests_made + '1' WHERE email = '$account1';");
echo"Request Is Served";
sleep(30);
mysqli_query($conn, "UPDATE hash SET check_speed = requests_made - '1' WHERE email = '$account1';");
?>
This works perfectly until the user decides to exit the tab until it finishes loading because the counter will update +1 on the requests_made. Any suggestions for these ?
Use the ignore_user_abort function.
So I am trying to write a php script that records one vote increment per user. Voters a are presented with a list of prospective candidates then clicks once on their name.
Now on attempt of a second time i hope to get a notification allowing this action. Please help.
<?php
if(isset($_POST['vote']))
{
$sql3='INSERT INTO sessions (memberID, postid, email, voted) VALUES ("","", "", 1;)';
$result3 = mysqli_query($con, $sql3);
}
// $count = mysqli_num_rows($result2);
$candidate_name = null;
$vote = $_POST['vote'];
mysqli_query($con, "UPDATE tbCandidates SET candidate_votes=candidate_votes+1 WHERE position_id='$vote'");
// $count = mysqli_num_rows($result2);
if ( $count<1) {
//$sql3='INSERT INTO sessions (memberID, postid, voted) VALUES ("", memberbers.memberID,"1")';
//$result = mysqlI_query("select id from Users where username ='".$_SESSION['email']."'");
//$result = mysqli_query($con, $sql3);
// $count = mysqli_num_rows($result2);
} else {
echo"You have voted already";
}
Put a "unique" flag on your memberID column in your SQL table. In that case when you try to insert a new row with the same memberID, it will not work.
Then catch the SQL write fail, and display a message if it gets to it.
I have a standard MySQL database, with around 60 rows (as in user accounts). When I first made it I made the mistake of making session IDs the same as the simple account ID, now I want to fix my mistake and I am obviously not going to go through 60 rows to reset them different secure session IDs, so I am writing this function:
function generate_sessionid(){
return bin2hex(openssl_random_pseudo_bytes(32));
}
function assign_all_sessionids(){
$sessionid = generate_sessionid();
$conn = sql_connect();
$result = mysqli_query($conn, "UPDATE accounts SET sessionid='$sessionid' WHERE 1");
sql_disconnect($conn);
}
assign_all_sessionids();
Problem: Every account in the database gets the same random session ID as the rest. How do I make it recall the function for each row in order to allow it to be random for each row?
Try get user's count from DB and simply execute it N times
function assign_all_sessionids(){
$conn = sql_connect();
// getting users count
// here just change 'id' to your id parameter
$result = mysqli_query($conn, "SELECT id FROM accounts");
$arr = $result->fetch_array(MYSQLI_NUM);
// executing N times
for($i = 0; $i < $result->num_rows; $i++){
$sessionid = generate_sessionid();
// here just change 'id' to your id parameter again
mysqli_query($conn, "UPDATE accounts SET sessionid='$sessionid' WHERE `id`=".$arr[$i]);
}
sql_disconnect($conn);
}
You can do what you want by first setting all the session ids to NULL:
UPDATE accounts
SET sessionid = NULL;
Then, inside the loop:
UPDATE accounts
SET sessionid = '$sessionid'
WHERE sessionid IS NOT NULL
LIMIT 1;
Normally you don't want to execute queries in a loop, however in this case you need to get all of the current unique identifiers, loop and generate a new identifier and then update one:
function assign_all_sessionids(){
$conn = mysqli_connect('whatever...');
$select = mysqli_query($conn, "SELECT sessionid FROM accounts");
while(list($id) = mysqli_fetch_assoc($select)) {
$sessionid = generate_sessionid();
$update = mysqli_query($conn, "UPDATE accounts SET sessionid='$sessionid' WHERE sessionid='$id'");
}
}
Basically, I have been having some trouble with sending a request to a MySQL server and receiving the data back and checking if a user is an Admin or just a User.
Admin = 1
User = 0
<?php
$checkAdminQuery = "SELECT * FROM `users` WHERE `admin`";
$checkAdmin = $checkAdminQuery
mysqli_query = $checkAdmin;
if ($checkAdmin == 1) {
echo '<h1>Working!</h1>';
}else {
echo '<h1>Not working!</h1>';
}
?>
Sorry that this may not be as much info needed, I am currently new to Stack Overflow.
Firstly, your SQL query is wrong
SELECT * FROM `users` WHERE `admin`
It's missing the rest of the WHERE clause
SELECT * FROM `users` WHERE `admin` = 1
Then you're going to need fetch the result from the query results. You're not even running the query
$resultSet = mysqli_query($checkAdminQuery)
Then from there, you'll want to extract the value.
while($row = mysqli_fetch_assoc($resultSet))
{
//do stuff
}
These are the initial problems I see, I'll continue to analyze and find more if needed.
See the documentation here
http://php.net/manual/en/book.mysqli.php
You need to have something like user id if you want to check someone in database. For example if you have user id stored in session
<?php
// 1. start session
session_start();
// 2. connect to db
$link = mysqli_connect('host', 'user', 'pass', 'database');
// 3. get user
$checkAdminQuery = mysqli_query($link, "SELECT * FROM `users` WHERE `id_user` = " . $_SESSION['id_user'] );
// 4. fetch from result
$result = mysqli_fetch_assoc($checkAdminQuery);
// 5. if column in database is called admin test it like this
if ($result['admin'] == 1) {
echo '<h1>Is admin!</h1>';
}else {
echo '<h1>Not working!</h1>';
}
?>
// get all admin users (assumes database already connected)
$rtn = array();
$checkAdminQuery = "SELECT * FROM `users` WHERE `admin`=1";
$result = mysqli_query($dbcon,$checkAdminQuery) or die(mysqli_error($dbconn));
while($row = mysqli_fetch_array($result)){
$rtn[] = $row;
}
$checkAdminQuery = "SELECT * FROM `users` WHERE `admin`"; !!!!
where what ? you need to specify where job = 'admin' or where name ='admin'
you need to specify the column name where you are adding the admin string
I have this field on my database
email, addition so what i want is that WHERE email='$email' it will look that the data user had for addition. now for addition field we have 100,100,1000
how can i compute this with the echo 1200
my code is NOTE this is just a sample scriptcode i can't copy and paste my code here because it was long. so if you find typo error ignore it.
$sql = mysql_query( "SELECT * FROM user WHERE email = '$email' ") or die(mysql_error());
while ( $row = mysql_fetch_array($sql) ){
echo $row[addition] ++;
}
yes i know its not correct format. what should i change? do i need to use for or foreach? in order to add those computation?
tghanks
with correct (normalized) data structure you can do:
SELECT SUM(addition) AS total FROM users WHERE email = '$email'
if you store it as csv in a column as Ignas says in his commentes,
you can either fix your database structure or use php:
$sum = array_sum(explode(',', $row['addition']));
Somthing like?
$sql = mysql_query( "SELECT * FROM user WHERE email = '$email' ") or die(mysql_error());
while ( $row = mysql_fetch_array($sql) ) {
$iAdditionSum = 0;
$aAddition = explode(',', $row['addition']);
foreach($aAddition as (int) $iAddition) {
$iAdditionSum += $iAddition;
}
echo $iAdditionSum;
}