I have done it a million times before and never failed, but this time I am making some mistake and can't figure out.
I am doing
$sql = 'update user set token=:token and token_time=:token_time where id=:id';
$query = $con->prepare($sql);
$query->bindValue(':token',$token,PDO::PARAM_STR);
$query->bindValue(':token_time',$current_datetime,PDO::PARAM_STR);
$query->bindValue(':id',$id,PDO::PARAM_INT);
if($query->execute())
{
var_dump($token);
var_dump($id);
var_dump($current_datetime);
}
and I am getting the dump as follows
string(32) "a10b15b88ed35551a17cbdf4e5db2624" int(1) string(19) "2015-03-09 15:50:18"
which means the fields are not null, query executed and I also checked, there is a row with id=1 in the table user.
But when I look into the table, the row with id=1 is not updated. why?
EDIT
MySQL table
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| fname | varchar(255) | YES | | NULL | |
| lname | varchar(255) | YES | | NULL | |
| email | varchar(255) | YES | | NULL | |
| password | varchar(255) | YES | | NULL | |
| contact | text | YES | | NULL | |
| token | text | YES | | NULL | |
| reg_time | datetime | YES | | NULL | |
| token_time | datetime | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
EDIT
As per #davidstrachan, the problem is in the update query. , should be used instead of and, and then it works fine.
Now the new question is why didn't MySQL give error?
Related
I have two tables as below:
Table Job_Announcement used to store information about Job, defined as below:
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| job_id | int(11) | NO | PRI | NULL | auto_increment |
| job_title | varchar(255) | NO | | NULL | |
| category | varchar(255) | NO | | NULL | |
| term | varchar(255) | NO | | NULL | |
| num_experiences | int(11) | NO | | NULL | |
| num_hiring | int(11) | NO | | NULL | |
| Salary | varchar(255) | NO | | NULL | |
| qualification | varchar(255) | NO | | NULL | |
| location | varchar(255) | NO | | NULL | |
| gender | varchar(255) | NO | | NULL | |
| job_content | text | NO | | NULL | |
| job_requirement | varchar(255) | NO | | NULL | |
| publish_date | date | NO | | NULL | |
| close_date | date | NO | | NULL | |
| contact_info | varchar(255) | NO | | NULL | |
| userid | varchar(255) | NO | | NULL | |
| publish | tinyint(1) | NO | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
And Table job_announcement_deleted, used to store deleted record from table job_announcement
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| job_id | int(11) | NO | PRI | NULL | auto_increment |
| job_title | varchar(255) | NO | | NULL | |
| category | varchar(255) | NO | | NULL | |
| job_content | text | NO | | NULL | |
| publish_date | date | NO | | NULL | |
| close_date | date | NO | | NULL | |
| userid | varchar(255) | NO | | NULL | |
| publish | tinyint(1) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
In order to move deleted record from job_announcement table to job_announcement_deleted table, I defined trigger definition in phpmyadmin like this:
Trigger Name: before_delete_job
Table: job_announcement
Time: BEFORE
Event: Delete
Definition:
BEGIN
INSERT INTO job_announcement_deleted VALUES(old.job_id,old.job_title,old.category,old.job_content,old.publish_date,old.close_date,old.userid,old.publish);
END
Definer: root#localhost
The trigger event before_delete_job is working fine that the deleted record moved to table job_announcement_deleted.
My problem is if I want to restore deleted record back to table job_announcement, I define similar trigger definition event like above, ex. job_announcement_restore for tale job_announcement_deleted however, how can I do if I want to delete record permanently from job_announcement_deleted? because I want user have options either to restore it or to deleted permanantly.
Thanks.
For the restore just do the reverse process you did for delete (sending data from job_announcement_deleted to job_announcement)
For the perma delete you just have to get the job_id field, since it's auto-incremeneted making it unique for every job.
After you acquire job_id from the user (e.g if he clicks delete from this row you extract job_id from that row), just use DELETE FROM like this :
DELETE FROM job_announcement_deleted WHERE job_id = #job_id
(where #job_id will be the one you acquired earlier in the process)
Also, you shouldn't be sending job_id to the job_announcement_deleted table, since the field is auto-increment, meaning it'll add a new value (incremented by 1 by default) for that field, so there are no duplicates for that field. You just destroy the meaning of auto-increment by doing that, specially since primary key must be a unique value. You could have trouble later on with the database if you get duplicate values for that field, so I suggest you just leave that field unfilled so auto-increment can do it's job.
I am using Entrust's default table structure:
permissions table:
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| display_name | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| created_at | timestamp | NO | | NULL | |
| updated_at | timestamp | NO | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
permission_role table:
+---------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+-------+
| permission_id | int(10) unsigned | NO | PRI | NULL | |
| role_id | int(10) unsigned | NO | PRI | NULL | |
+---------------+------------------+------+-----+---------+-------+
roles table:
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| display_name | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| created_at | timestamp | NO | | NULL | |
| updated_at | timestamp | NO | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
Now, given a role_id I'd like to get select the following from this database:
permissions.id
permissions.display_name
whether the permission_role table contains an entry with the permission_id and the given role_id
The last one turned out to be a bit tricky in Eloquent.
This SQL query accomplishes exactly what I need (ID is obviously replaced by a valid role ID):
SELECT p.id, p.display_name, IF(pr.role_id = ID, 1, 0) AS has_role
FROM permissions p
LEFT OUTER JOIN permission_role pr ON p.id = pr.permission_id;
Example output:
+----+--------------+----------+
| id | display_name | has_role |
+----+--------------+----------+
| 1 | Edit users | 1 |
| 2 | View users | 0 |
| 3 | Delete users | 0 |
+----+--------------+----------+
Can anyone help me out here, on how to do this using Eloquent?
I've tried this, but it always returns 1 (true) in the third column, unlike the SQL query (as seen above).
$result = DB::table('permissions')
->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permission_role.role_id')
->select(DB::raw('permissions.id, permissions.display_name, IF(permission_role.role_id = ID, 1, 0) AS has_role'))
->get();
Ideally, I'd like to do this without using DB::raw, although it is completely fine if that is what it takes.
Thanks in advance for any help!
Structurally, the Query Builder query you've shown looks fine.
What does not look fine is the left join. Shouldn't this:
->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permission_role.role_id')
be this:
->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permissions.id')
?
I have been looking if an this has been answered before in SO, but I could not find any.
I have basically two tables in one database. Table member_privileges1 keeps users reputation, and table 2 keeps user details. They look roughly like this.
mysql> describe member_privileges;
+---------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(255) | YES | | NULL | |
| can_comment | int(11) | NO | | NULL | |
| can_create_articles | int(11) | NO | | NULL | |
| can_edit_articles | int(11) | YES | | NULL | |
| can_edit_timeline | int(11) | NO | | NULL | |
| can_remove_comments | int(11) | NO | | NULL | |
| can_upload_profile_images | int(11) | NO | | NULL | |
| can_upload_article_images | int(11) | NO | | NULL | |
| can_approve_new_articles | int(11) | NO | | NULL | |
| can_freez_articles | int(11) | NO | | NULL | |
+---------------------------+--------------+------+-----+---------+----------------+
and this is the users table.
mysql> describe users;
+----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(255) | NO | | NULL | |
| password | varchar(255) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
| country | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | | NULL | |
| link | varchar(255) | NO | | NULL | |
| reg_date | date | YES | | NULL | |
| ip | varchar(255) | YES | | NULL | |
| gender | varchar(10) | YES | | NULL | |
| about | varchar(255) | YES | | NULL | |
| is_activated | int(11) | YES | | NULL | |
| activation_key | varchar(255) | YES | | NULL | |
| image_path | varchar(255) | YES | | NULL | |
+----------------+--------------+------+-----+---------+----------------+
So, in one page for example, I am trying to check if user 'john' has a can_edit_articles permission set. So, if in member_privileges a username "john" has a can_edit_articles set to 1, then that user can edit.
The problem is checking those details at one. Unlike running two queries like I am doing it now.
$email = $_SESSION['email'];
$get_user = "SELECT id, username FROM members WHERE email = $email;
$can_user_edit = "SELECT can_edit_articles FROM member_privileges WHERE $email = ?;
So, as you can see I am running two queries, and I would like to know if there is any way to run those within one mysql statement.
...
Use an inner join and prevent your sql statements from sql injection
If you would like to select the id, username and can_edit_articles values:
"SELECT m.id, m.username, p.can_edit_articles
FROM members m inner join member_privileges p
ON m.username=p.username WHERE m.email = '$email'"
$email = $_SESSION['email'];
$can_user_edit = "SELECT can_edit_articles FROM member_privileges
WHERE username = (SELECT username FROM users WHERE email = '$email')";
You should use JOIN in your query. Like this:
SELECT can_edit_articles FROM member_privileges
P LEFT JOIN members M ON P.username = M.username WHERE email = '$email'
Suppose I have two tables, one with list of spells and another with a grimory, the list of spells that a user has selected for learn or already learned.
mysql> SHOW COLUMNS FROM Grimory;
+--------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| personage_id | int(11) | YES | MUL | NULL | |
| spell_id | int(11) | YES | MUL | NULL | |
| isLearned | tinyint(1) | NO | | NULL | |
| isSelected | tinyint(1) | NO | | NULL | |
+--------------+------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> SHOW COLUMNS FROM Spell;
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| description | longtext | NO | | NULL | |
| chatDescription | longtext | NO | | NULL | |
| level | int(11) | NO | | NULL | |
| isActive | tinyint(1) | NO | | NULL | |
| category_id | int(11) | YES | MUL | NULL | |
| createdAt | datetime | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
The problem.
I want to display a list with ALL spells by category_id, BUT for every row I want to show if that spell is learned or selected by current user (personage_id).
Can you help me to write a proper query?
How this works:
With php I generate: List of spells | checkbox isSelected | checkBox isStudied
When I click on isSelected checkbox, a record in grimory will be added with current user and spell.
Updated
SELECT a.*, IFNULL(b.isLearned,0) as isLearned,
IFNULL(b.isSelected,0) as isSelected
FROM Spell a
LEFT JOIN Grimory b(ON b.spell_id =a.id
AND b.personage_id =:current_user_id)
WHERE a.category_id = :current_category_id
SELECT * FROM Spell
INNER JOIN Grimory
ON Spell.id = Grimory.spell_id
WHERE (Grimory.isLearned = 1 OR Grimory.isSelected = 1)
AND Spell.category_id = 'YOUR CAT ID IS HERE'
I have the following MySQL query and tables from which I am querying:
SELECT
`Song`.`id`,
`Song`.`file_name`,
`User`.`first_name`,
`Vote`.`value`,
Sum(`Vote`.`value`) AS score
FROM `songs` AS `Song`
LEFT JOIN votes AS `Vote` ON (`Song`.`id`=`Vote`.`song_id`)
LEFT JOIN `users` AS `User` ON (`Song`.`user_id` = `User`.`id`)
GROUP BY `Vote`.`song_id`
LIMIT 20;
mysql> describe songs;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| file_name | varchar(255) | NO | | NULL | |
| user_id | int(11) | NO | | NULL | |
| created | datetime | NO | | NULL | |
| modified | datetime | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> describe users;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(255) | NO | | NULL | |
| password | varchar(255) | NO | | NULL | |
| first_name | varchar(255) | NO | | NULL | |
| last_name | varchar(255) | NO | | NULL | |
| is_admin | tinyint(1) | NO | | 0 | |
| created | datetime | NO | | NULL | |
| modified | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
mysql> describe votes;
+----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| value | int(11) | NO | | NULL | |
| song_id | int(11) | NO | | NULL | |
| user_id | int(11) | NO | | NULL | |
| created | datetime | NO | | NULL | |
| modified | datetime | NO | | NULL | |
+----------+----------+------+-----+---------+----------------+
This query functions just like I want except for one thing. The value returned in the field Vote.value is not from a row that is associated with the user who is logged into the application. I need the score value to be a sum of all the values no matter which user it is associated with, but the Vote.value field should belong to the logged in user (each user only gets one vote record per song).
My first thought is to somehow sort the table so that when the group by happens the vote record for the logged in user is at the top but I have no idea how to do a sort that forces an arbitrary value to the top. Any ideas would be very helpful.
and a third join
LEFT JOIN votes AS `VotePerUser` ON (`Song`.`id`=`Vote`.`song_id`
AND `Song`.`user_id`=`votes`.`user_id`)
and replace the Vote.value with VotePerUser.Value