create an event on PhpMyAdmin - php

i have a table named request where i store name, last name,user_id, category amount and points. its a money request i made so amount can vary all the time.
and i have a table named user. that stores user id, category, name, last name
what i want to accomplish is create an event that triggers everyday at 23:00 and search for all user based on there category and update the table user if it finds any user category with he following condition.
category id 1,2,3 all user at the begin are 1, if user category is 1 and points are = 4000 then update table user by changing user category to 2 example
<?php
$points = data->info_from_db;
$user_category = data->info_from_db;
if ($user_category == 1 && $points == 400)
{
///update users set category ='2'
}
else {
echo "no update today";
}
?>
this can be done by php? all i need is to first get all sum(points) of each user and their category and update if the condition is true.

Related

parent and child database schema

I have one table named as user
USER(Table)
user_id name status_privilege(0=child,1=parent) added_user_id
2 abc 1
13 child_1 0 2
14 child_2 0 2
I want display user_id = 2(parent's) data in all user_id = 14,15 (all child's login). And also I want to display user_id = 14 , 15's data in user_id = 2's data. (Viceversa)
So for that what to do ?
I tried this query.
SELECT *,child.user_id as child_user_id,parent.user_id as parent_user_id
FROM user as parent
INNER JOIN user as child ON parent.user_id = child.user_added_id
WHERE child.user_id = '14'
so I got child_user_id = 14 & parent_user_id = 2
But I want that when I put below query it give me blank result. (changed child.user_id = '2')
SELECT *,child.user_id as child_user_id,parent.user_id as parent_user_id
FROM user as parent
INNER JOIN user as child ON parent.user_id = child.user_added_id
WHERE child.user_id = '2'
So I want is that when I fire query single query then if user is parent than also I want it's child's all data in user_id ='2' means data of 14 & 15 user's. And 2nd user_id's data in all childs.
Note : All the data in other tables are from user_id so is it possible ?
It's a requirement of existing project so I can not change database structure otherwise I can follow MySQL hierarchical of parent child structure.
the answer is based on what i understand from the question is:
you want the full array of child user ids if parent user logged in and only one parent id if child user logged in.
so in php you can derived that via
<?php
$user=array(); // this array will contains the user info
$res=$db->query("select * from user where user_id='$id'"); //$id the user id with which you want to check whether it is parent or child.
if($res->num_rows > 0)
{
$row=$res->fetch_assoc();
if($row['status_privilege']==0)
{
$res_main=$db->query("SELECT *,child.user_id as child_user_id,parent.user_id as parent_user_id FROM user as parent INNER JOIN user as child ON parent.user_id = child.user_added_id WHERE child.user_id = '".$row['user_id']."'");
$user=$res_main->fetch_assoc();
}
else
{
$res_main=$db->query("SELECT * from user where added_user_id='".$row['user_id']."'");
$user=$row;
$user["child_array"]=array();
while($row_child=$res_main->fetch_assoc())
{
array_push($user['child_array'],$row_child['user_id']);
}
}
}
//you can further check that if user is parent or child by using this condition.
if($user['status_privilege']==1)
{
//parent login it also contain the all child user ids
}
else
{
//child login it also contain the parent id as you have done it before.
}
?>

SELECT query is only SELECTING/CHECKING the first row, how do I SELECT/CHECK all

I have this simple query:
$q5 = "select listingid FROM userlisting WHERE userid = '$_SESSION[UserID]'";
$r5 = mysql_query($q5) or die(mysql_error());
$a5 = mysql_fetch_array($r5);
The userlisting table is a many to many relationship. It is used as a lookup table. Each userid can be associated with multiple listingids and vice versa.
Also in my file (a html template file) I have this code
if(!empty($_SESSION[UserID]) and $a5['listingid'] == $_GET['id']) :
So I am wanting to check if the listingid column in userlisting table = the id of the page (well, it is a property website and it is the id of the property). As each user can be associated with many listingids I need to be able to check multiple rows. But for some reason it only checks the very first row.
In the userlisting table there is the following test entries:
userid | listingid
1 1
1 2
So one user associated to two listingids. However, it is acting like this userid is only associated with listingid 1, the very first row.
You are only grabbing the first entry.
$a5 = mysql_fetch_array($r5);
This does not fetch the whole query result, but only the first row. If there is none, it will return false.
You have to create a loop to get more results.

How does if else work?

How do i start on an if else statement to restrict?
Fields in database :
No_of_vacancy (shows the number of positions available in the company)
Assigned (shows the number of student assigned to company)
EG
ABC Co
No of vancacy = 2
Assigned student = 2
I have done the necessary statement
UPDATE job_details,
student_details
SET
assigned = assigned + 1
WHERE
student_details.jobscope1 = job_details.jobscope
AND student_details.jobscope1 = 'IT';
The above statement works fine. meaning each time query runs. 1 student will be added under assigned field in database. I want the query to stop after assigned field matches the no of vacancy field to avoid duplication.
My logic is that i have to use if else statement to restrict the amount of assigned students from going over the no of vacancy available which is 2.
how do i start?
And if you simply add, to the where, and (assigned + 1) < job_details.No_of_vacancy ?

Let user create table based on existing table

After registering at my site, I want the user to select some elements from a table that already exists in my DB, and add them to "their" column in another table.
Say this is the existing table in a MySQL DB:
ID Item Color
1 Car Red
2 Apple Green
3 Trophy Gold
4 Suit Black
I would want the user to fill out a form where they:
Are presented with a dropdown list, to choose items from (based on the existing table).
When they have chosen up to x amount of items, they submit their "inventory" which..
Adds the selected items to their own column in a table that holds "user_inventory"
So this second table (user_inventory) should look something like this:
User_id item_id1 item_id2 item_id3
1 4 3 1
2 3 1 4
3 2 4 1
4 2 4 3
I don't expect you to write the code for me or anything, but I would be thrilled if you could answer these questions:
Is this possible?
Can you direct me to a similar type of thread or article that helps me write the code?
If you can not direct me anywhere, please help me in whichever way you see fit.
It is possible.
Firstly, read from the "existing" db table to create the select menu, example:
<select name="item">
<?php
$sql = "SELECT * FROM existing";
$query = mysqli_query($mysqli, $sql);
while ($result = mysqli_fetch_array($query)) {
$item = $result['item'];
echo "<option value='$item'>$item</option>";
}
?>
</select>
This will create the "Item" select menu, I'm sure you can figure out how to do the other menus by yourself, all that is left now is to submit and store into the new table which I assume you already know how to do.
if(isset($_POST['yoursubmitname'])) {
$item = $_POST['item'];
/// RUN THE INSERT COMMAND HERE //
}

database trouble - same values in different rows

I am trying to make my voting system work. I have 3 databse tables:
users, posts, votes
the table users has field username as the primary key. table post has post_id as the primary key. (there are more fields but they don't affect the question/problem)
In the votes table I have 3 fields: username, post_id, vote. vote is enum ('positive', 'negative'). What I'm trying to achieve is that if a user votes for a specific post that is displayed on a page, the query: INSERT INTO votes ('username','post_id','vote') VALUES('$user_name','$post_id', 'positive'); will be executed.
It works if lets say user 123123 has not voted for any post at all yet. When this user votes lets say for post 1, this query works fine. But then if this user wants to vote for a different post, (his vote gets counted - I just copied the part of the code that doesn't work, the rest of it is fine and working) the insert query get's not executed. If user abcd wants to vote for a specific post, this works fine again, but only once. It seems to me that there is some kind of problem with the database, so that there can be only one entry with the same username or post_id. How can I fix this if I want one user to be able to vote for multiple posts? Is there a better strategy for this?
if($runloggedin->num_rows == 1)
{
// If there was no vote for the current posting, then execute this query
$query = "SELECT * FROM posts WHERE post_id='".$post_id."' AND user_name='".$user_name."'"; //get username and the post id
$result = $mysqli->query($query);
$query1 = "SELECT * FROM votes WHERE post_id='".$post_id."' AND username='".$user_name."'"; //check if there is a vote for this post already
$result1 = $mysqli->query($query1);
if ($result->num_rows == 1 && $result1->num_rows == 0)
{
$vote = "INSERT INTO votes ('username','post_id','vote') VALUES('$user_name','$post_id', 'positive')"; // this isn't working. everything else seems to be working (still test it more)
$savevote = $mysqli->query($vote);
$addvote = "UPDATE posts SET posvotes=posvotes+1 WHERE post_id='".$post_id."'";
$runvote = $mysqli->query($addvote);
echo "Thank you for your vote";
}
}
Without seeing how your votes table was created, my guess is that username has been set up as the primary key. This will make the first INSERT work, but all future ones fail. What you need to do is change it to have username & post_id be the primary key
ALTER TABLE `votes` DROP PRIMARY KEY , ADD PRIMARY KEY ( `username`, `post_id` )

Categories