My database system is pretty easy. I have a table with the users, and a table with 'guesses'. My site has logos, and a user can guess the name of each logo. There is a unique relation between the logo_id and the user_id. Here's the guesses table-structure:
|| id | user_id | logo_id | guess | guess_count | guessed | level | time ||
Now, when a user visits the page and isn't logged in, a user is made with the session_id and all the guesses and stuff are stored in the same structure. So the only difference is in the user-table.
Now I want the user to be able to login and keep whatever he/she just did. Of course, if the logged in account already has the logo as guessed, this shouldn't be altered. But when the logged in account has a logo as not-guessed, the guess_count of the session user should be added to the logged in user guess_count. Also the guessed should be updated. It should only do this if the time of the edit was more recent, but I think it's safe to assume that the session-guesses are more recent.
Now how I would do this, is loop through all the logos from the logged in id first where guessed = 0, then for each result do a query again to add the guess_count and store the guessed, then remove all the ones found from the session-id, then loop through all the old ones with the session-id and change the user_id to the one of the logged-in-user. Now, this is a ton of queries, so there must be a more efficiƫnt way.
Any thoughts? Sorry for the wall of text & bad explanation, databases are not my best thing.
Sorry havn't used mySQL in donkey's years, but here's an example of the stored procedure in SQL Server: Hopefully someone can help with the MySQL syntax, or you can infer it from the SQL below
CREATE PROC MergeGuesses
#UserSessionId INT,
#UserId INT
AS
--where the userId has already a guess for the logo update it
UPDATE gusr
SET gusr.guess_count = gusr.guess_count + gses.guess_count,
gusr.guessed = gses.guessed
FROM Guesses gusr
JOIN Guesses gses ON gusr.logo_id = gses.logo_id
AND gusr.time > gses.time -- more recent
WHERE gusr.user_id = #UserId
AND gses.user_id = #UserSessionId
AND gses.guessed = 0 --not yet guessed
--where there is no guess for the user yet - just update the userId
UPDATE gses
SET gses.user_id = #UserId
FROM Guesses gses
LEFT JOIN Guesses gusr ON gusr.logo_id = gses.logo_id
AND gusr.user_id = #UserId
WHERE gses.user_id = #UserSessionId
AND gusr.user_id = NULL -- there is no guess for the userId
--finally delete any rows for the sessionId that are left
DELETE FROM Guesses
WHERE user_id = #UserSessionId
Since I have no idea how to start stored procedures, I've just written it out in a few queries, but it's okay I guess.
$STH = $DBH->prepare("SELECT logo_id, guess_count, guessed, guess FROM guess WHERE user_id=:id GROUP BY logo_id");
$STH->bindParam(":id",$loginuser['id']);
$STH->execute();
while($row = $STH->fetch()){
if($row['guessed'] == 0){
$STH2 = $DBH->prepare("SELECT guess, guess_count, guessed FROM guess WHERE logo_id=:logo_id AND user_id=:id");
$STH2->bindParam(":logo_id",$row['logo_id'],PDO::PARAM_STR,20);
$STH2->bindParam(':id',$_SESSION['login'],PDO::PARAM_INT);
$STH2->execute();
$row2 = $STH2->fetch(PDO::FETCH_ASSOC);
if($row2){
$STH3 = $DBH->prepare("UPDATE guess SET guess_count=guess_count+:guess_count, guessed=:guessed, guess=:guess WHERE logo_id=:logo_id AND user_id=:id");
$data = array('guess_count'=>$row2['guess_count'],'guessed'=>$row2['guessed'],'guess'=>$row2['guess'],'logo_id'=>$row['logo_id'],'id'=>$loginuser['id']);
$STH3->execute($data);
$STH3 = $DBH->prepare("DELETE FROM guess WHERE logo_id=:logo_id AND user_id=:id");
$STH3->bindParam(":logo_id",$row['logo_id']);
$STH3->bindParam(':id',$_SESSION['login']);
$STH3->execute();
}
}else{
$STH2 = $DBH->prepare("DELETE FROM guess WHERE logo_id=:logo_id AND user_id=:id");
$STH2->bindParam(":logo_id",$row['logo_id']);
$STH2->bindParam(':id',$_SESSION['login']);
$STH2->execute();
}
}
$STH = $DBH->prepare("UPDATE guess SET user_id=:login WHERE user_id=:session"); // update all entries that are new from session
$STH->bindParam(':login',$loginuser['id']);
$STH->bindParam(':session',$_SESSION['login']);
$STH->execute();
Related
DISCLAIMER: I'm still new to this website so I'm still learning the etiquette of the site, I apologize for any errors. Also, I previously posted a questions similar to this but some fantastic people recommended I rework my database to the current format. This was a great help however it was one step forward and one step back. I have an improved database but my question now continues to stand with a few minor tweaks.
To elaborate, I'm currently building an app that has the user create an account and login. Their information that they provided is saved into my database. My database contains two tables, one holds the users information, and one holds the users inventory, both are generated upon the completion of a create account GUI. For this question, only the second table is necessary. This table has three columns, the first is the users username, the second is their inventory slot number, and the third is the item id for the item that is in that slot. When the user creates an account, forty rows are created in this table, in each row their username remains constant. However, the slot number increments from one to forty and the item id column defaults to zero. Here is a visual representation:
Now to get to my code, when the user clicks a button, a random method gets called which sets an int variable which is current named "i" to a specific number. This number is the ID of an item in my app. At this point the user is prompted with two buttons that ask whether they want to keep the item or discard it. If they decide to keep the item I need it to be added to their inventory in the database. This is where my question comes into play. My app knows which user is logged in because when someone properly logs in the app sets their username (which is a primary key) to a global string variable which the rest of the app can user. so it knows which user to update but I need it to check through each of the rows in order, and if it finds a row with a zero in the ItemID column, it will update it to what the variable "i" currently is and end the query.
This is my current code, I'm very new to SQL but I'm trying to teach myself, I apologize if this offends you (because it's so bad):
EDIT: I've updated my code to this new query however I get an error that states java.sql.SQLException: You can't specify target table 'userinv' for update in FROM clause
try{
//get connection to database
Connection con = DataBaseConnect.getConnection();
//create a statement
PreparedStatement addInfo = con.prepareStatement("UPDATE userinv SET "
+ "ItemID = "+i+" "
+ "WHERE Username = '"+LoginController.userLog+"' "
+ "AND Slot = ("
+ "SELECT MIN(Slot) FROM userinv "
+ "WHERE ItemID = 0 "
+ "AND Username = '"+LoginController.userLog+"')");
//process result set
addInfo.executeUpdate();
}
catch(Exception e){
e.printStackTrace();
}
at this point I know it needs to update the userinv table and I know it needs to do this where the users username is but I'm not sure how to write the code in between. Does anyone have any ideas?
This works in Oracle and should work for MySql:
update userinv set itemid = 815
where username = 'test'
and slot = (
select min(slot) from userinv
where itemid = 0
and username = 'test'
)
For more complex cases where you need the first row according to some ordering, but can't express this as a minimum this approach works on Oracle:
update userinv set itemid = 815
where username = 'test'
and slot = (
select slot from (
select count(*) over (partition by username order by slot) cnt,
slot
from userinv
where itemid = 0
and username = 'test'
) where cnt = 1
)
It uses analytic functions so it won't work on MySql, but there is an article how to fake them in MySQL.
With analytic functions, this should also work (didn't try, so it does contain typos and stuff)
update (
select count(*) over (partition by username order by slot) cnt,
u.*
from userinv u
where itemid = 0
and username = 'test'
order by slot
)
set itemid = 815
where cnt = 1
This accesses the table only once, which should be way faster when your table is huge.
I'm working something with mysql and php and I'm trying to achieve some result for learning purposes.
So what I'm trying is to make conversation messages system and I have following:
I have 2 tables, first conversation and second conversation_messages
First table conversation looks like following:
c_id, user_one, user_two
Second table conversation_messages looks like this:
m_id, text, date, created_by, status, c_id
So in messages table I set Conversation ID and when user click conversation to open it, url change to messages.php?c_id=1 or something like that... And that's fine, becouse I get c_id from url and so on.
My question is following:
Lets say I wan't to get all messages for conversation c_id = 1. How do I query trough table and get all messages for that conversation id. Also I need to query so it return results only if logged user is involved into conversation... So logged in user can see conversation messages only if he is person/user A (user_one) or user B(user_two). How do I do that and do I need to join tables. So what is the best way to do this.
So when logged in user type manually into url messages.php?c_id=3 if he is not involved into that conversation I don't want him to see it.
Sorry I'm new here and don't know how to format code properly or anything.
Thanks in advance.
You need to get the logged user id from a session and put into the query like that
SELECT * FROM conversation_message, conversation
WHERE conversation.c_id = $ID_OF_CONVERSATION
AND (user_one = $ID_LOGGED_USER OR user_two = $ID_LOGGED_USER)
AND conversation_message.c_id = conversation.c_id
In the broad strokes, if you want to add security to a certain endpoint, you need to allow or deny access after validating user input. In the example you gave, your user input is the c_id value of 3. In a simple PHP example, you could do something like this:
$user_id = $_SESSION['user_id'];
$can_access = false;
$convo_id = $_GET['c_id'];
$safe_id = mysql_real_escape_string( $convo_id );
$rli = mysql_query( "SELECT * FROM conversation WHERE c_id = {$safe_id}" );
if( mysql_num_rows( $rli ) ) {
$convo = mysql_fetch_object( $rli );
$can_access = $convo->user_one == $user_id || $convo->user_two == $user_id;
}
Notice in this example that I pulled the "logged in" user's id from the session, which assumes that you are using sessions. There are many different ways to create "logged in" user views, and that is somewhat outside the scope of this answer. The end result here is a boolean value variable $can_access which indicates whether or not the user can access the page. Assuming they can access the page, you could pull all the messages from the now validated conversation like so:
$rli = mysql_query("SELECT * FROM conversation_messages WHERE c_id = {$safe_id}");
$messages = array();
while( $message = mysql_fetch_object( $rli ) ) {
$messages[ $message->m_id ] = $message;
}
The above gives you a PHP array containing all the messages associated with the conversation. Hope this is enough to get you started.
I have a select statement:
SELECT id, content, name
FROM records
WHERE type = '1'
AND name = 'test';
Here's the output:
id content name
99708 10.6.252.41 server01.example.org
What I'd like to do is be able to get the id that is returned from the previous statement and USE the id as input into another statement (an UPDATE statement) that will increment the value of a single column in the same table.
An example UPDATE statement that I am wanting is:
update records SET hits = hits + 1 WHERE id = ID_FROM_SELECT;
Thanks in advance.
You can use user defined session variables for this if the SELECT is returning just one result:
SELECT #id:=id AS id, content, name
FROM records
WHERE type = '1'
AND name = 'test';
Then, on the same database session (connection), do the following:
UPDATE records
SET hits = hits + 1
WHERE id = #id;
I'm assuming you're doing something with the selected records in your app, and you're trying to save on performance by avoiding having to search for the record again in the UPDATE. Though, in that case, why not set the 'id' value as a parameter in code?
Obviously, if the SELECT is returning multiple records, this would best be done in code as I mentioned above, otherwise you're left with running the SELECT query again as a subquery:
UPDATE records
SET hits = hits + 1
WHERE id IN
(SELECT id
FROM records
WHERE type = '1'
AND name = 'test');
So, then, it makes more sense just to apply the same filter to the UPDATE instead:
UPDATE records
SET hits = hits + 1
WHERE type = '1'
AND name = 'test'
Probably this is not what you want to do.
First of all...If the query only returns 1 line, the solution provided by Marcus Adams works fine. But, if the query only returns one line, you dont need to preset the id in order to update. Just update it:
update records
set hits = hits + 1
where type = '1'
and name = 'test'
Second...If the query will not return only one record and you want to update all records returned with same values or calculations, the same code above will do what you need.
Third, if the query does not return just one record and you need to update each record returned with different value then you need to have a different approach.
I think you are not designing your system very well. If the request for update come from outside, you should have the id to be updated as a parameter of your request. For example something like:
<html>
<body>
Test
</body>
</html>
And in your update.php you have something like:
<?php
$id = $_GET['id'];
$sql = "update records set hits = hits + 1 where type = '1' and name = 'test' and id = $id";
?>
Of course, the picture I have is to small. Probably you have a reason to do this way or this is just an example. If you fill us up with more info we might be more helpful.
I have 3 tables,
user - contains user_id | username | fullname | email etcc
user_profile - contains entry_id |user_id| profile_image_id| user_location etcc
user_profile_images - contains image_id| user_id | file_path | file_thumb | date etcc
When a user sign's up, all the details go to the users table, the user_id is also added to the user_profile table with the rest set default as NULL till the user add's some profile info.
I have a query in model_users to get all user's data from these three table and it goes like this;
$this->db->where('users.user_id', $user_id)->select('*')->from('users');
$this->db->join('user_profile', 'user_profile.user_id = users.user_id', 'left');
$this->db->join('user_profile_images','user_profile_images.image_id = user_profile.profile_image_id','left');
It work's fine only when the profile_image_id field in user_profile is not null, that is when the user has uploaded a pic. In the case where the field is null, the user user_id is not returned even when all the other data are returned.
I can see why this is the case, as my join query requires the field profile_image_id but currently my way around it was to set profile_image_id in user_profile as 1(default), which is the default image and has image_id as 1 and user_id as 0 as it's the general default image. But i still can't get over the fact that i need to update that query to make it less of a hack.
Do you guys have any ideas?
Try
$this->db->select('*')->select('users.user_id')->from('users')
->join('user_profile', 'user_profile.user_id = users.user_id', 'LEFT')
->join('user_profile_images', 'user_profile_images.image_id = user_profile.profile_image_id', 'LEFT')
->group_by('users.user_id')
->where('users.user_id', $user_id);
$query = $this->db->get();
When using join for tables you need your columns in all joined tables to have unique names. Either you add column aliases by AS syntax or just retrieve the columns you need. In your case the error is most probably due to the fact that user_id exists in all three tables. Hence the result is like you experience. user_id is retrieved only for the last occurrence and that is if your user has uploaded an image. I would alter the query someting like:
$this->db->where('users.user_id', $user_id);
$this->db->select('users.*,user_profile.user_location,user_profile_images.filepath'); // and other fields of interest...
$this->db->from('users');
$this->db->group_by('users.user_id'); // very useful for not getting multiple rows/user
$this->db->join('user_profile', 'user_profile.user_id = users.user_id', 'left');
$this->db->join('user_profile_images','user_profile_images.image_id = user_profile.profile_image_id','left');
Try This one.
$this->db->select("*");
$this->db->from('table1');
$this->db->join('table2','table2.id1=table1.id1');
$this->db->join('table3','table3.id1=table1.id1');
$query = $this->db->get();
return $query->result();
I am trying to return a members firstname field from the table users from the last row of users.
my $dbh = DBI->connect($dsn, $db_user_name, $db_password) or die "$DBI::errstr";
my $LastID = $dbh->last_insert_id(`firstname`); ##but I want from table users
print qq~$LastID~;
This error is returned from above:
DBI last_insert_id: invalid number of arguments: got handle + 0, expected handle + between 4 and 5
Usage: $h->last_insert_id($catalog, $schema, $table_name, $field_name [, \%attr ])
So, what would be the "best" way (best being best for server, fastest, least memory, load, least amount of overhead.. whatever) to get the field firstname from the last row in the table users?
Realize my example above is not to be taken seriously as I have no idea how to do this without just doing something like my crude, but functional:
(p.s. UserID is NOT assigned by auto increment but, is in numeric order and a new user gets a higher UserID. Just the way this was when I tackled the pre existing problem.)
my $dbh = DBI->connect($dsn, $db_user_name, $db_password) or die "$DBI::errstr";
my $dsn = $dbh->prepare(qq{SELECT `firstname` FROM `users` ORDER BY ABS(UserID) DESC LIMIT ?,?});
$dsn->execute('1','1') or die "$DBI::errstr";
while(#nrow = $dsn->fetchrow_array()) {
$firstname = $nrow[0];
}
I assumed since I was using DBI that may provide the best solution but, I am inexperienced obviously and need some advice and guidance to learn the proper way to do this. Thanks for any assistance.
You mention that UserID is not auto incrementing, so I'm not sure if last_insert_id will work in this situation. It may, but I'm just not sure. The document states:
Typically this would be a value assigned by the database server to a
column with an auto_increment or serial type.
I would look to solve this by just using a SQL statement:
SELECT
u.firstname
FROM
users u
JOIN
(
SELECT
MAX(UserID) AS UserID
FROM
users
) t ON u.UserID = t.UserID
The code with DBI would then look like this:
my $stmt = 'SELECT u.firstname FROM users u JOIN(SELECT MAX(UserID) AS UserID FROM users) t ON u.UserID = t.UserID';
my $first_name = ($dbh->selectrow_array($stmt))[0];
last_insert_id method takes 4 args. Use like this:
my $id = $connection->last_insert_id(undef, 'myschemaname', 'mytablename', 'mycolumnname');
See the DBI pod.