Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a database of 2 tables users and classes each user may have many classes so I added a third table called userClasses which had user ID and class ID , so when I want to delete the user from users table, I need to delete it from the UserClasse table too.
And I did that request :
$id = $_GET['id']; // already passed through URL using GET method .
$del = $db->prepare("DELETE FROM users,userClasses WHERE users.id= :id, userClasses.userID= :id");
$del->bindParam(':id',$id);
$del->execute();
But it won't DELETE! Nothing happens, is there any solution please & how ?
Your SQL is nonsense - where is the 'salleresp' table?
You can't delete a joined result set. Either declare your tables using foreign key constraints or run 2 delete queries - one for users and one for userClasses.
If there is a foreign key constraint you will not be able to delete entries from both tables in one query. However, if there isn't such a constraint, the following would work:
DELETE u.*, uc.*
FROM users u
LEFT JOIN userClasses uc
ON u.id = uc.userID
WHERE u.id= :id
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am php beginner.
I would like to read email contain in table 1 and with the value selected deleted the corresponding email of the table 2 ?
May you provide me some info to manage it ?
select email_table1 from table1.
delete from table2 where email = email_table1.
Thanks
Greg
There's little information to form an answer but you could use a sub select.
Something like this presuming you're using MySQL or similar.
DELETE FROM `table1` WHERE id = (
SELECT `referenceID` FROM `table2` WHERE `email` = 'the#email.com' LIMIT 1
) LIMIT 1;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i got
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'as idab, b.id_skb as idab FROM skd a, skb b WHERE idab='5'' at line 1** when i try to delete data from id by 2 tables...
here it is,
<?php
include './../../koneksi.php';
$idab= $_GET['idab'];
if (mysqli_query($koneksi, "DELETE a.id_skd as idab, b.id_skb as idab FROM skd a, skb b WHERE idab='$idab' "));{
echo ('<script>
alert("Data Anda Berhasil Dihapus...!!!");
window.location="http://localhost/administrasi_kelurahan/pengguna/administrator/Riwayat_Notif.php";
</script>');
}
?>
Perform two DELETE queries, not one. Use a transaction to make it atomic.
$stmt1 = mysqli_prepare($koneski, "DELETE FROM skd WHERE idab = ?");
mysqli_stmt_bind_param($stmt1, "s", $idab);
$stmt2 = mysqli_prepare($koneski, "DELETE FROM skb WHERE idab = ?");
mysqli_stmt_bind_param($stmt2, "s", $idab);
mysqli_query("START TRANSACTION");
mysqli_stmt_execute($stmt1);
mysqli_stmt_execute($stmt2);
mysqli_query("COMMIT");
I've also shown how to use a prepared statement to prevent SQL injection.
You can't do it in a single query unless there's a relationship between the tables, such as a foreign key. In that case you would probably make use of ON DELETE CASCADE to automatically delete from the child table when you delete from the parent table.
If the data is related, you can use a JOIN
DELETE a, b
FROM skb AS a
JOIN skb_pengikut AS b ON a.id = b.id_skb
WHERE a.id = ?
But if you declare id_skb as a foreign key with the ON DELETE CASCADE option, then you just have to do:
DELETE FROM skb
WHERE id = ?
and it will automatically delete from id_pengikut.
When trying to Delete from two tables, you should use a transaction to couple the two seperate delete operations with a clean rollback possibility. Otherwise you might be left with the record deleted in one table an not deleted in the other.
If the two tables a linked by referencial integrety, then first delete it in referencing table an then in the referenced table.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am building a tourism platform and I am planning to have the following users table:
USERS: id, username, password, email, first_name, last_name, description, picture_path
My goal is to have two groups of users:
tour operators
tourists
When a tour operator logs in, I want the system to recognize that he is from the group "tour operators" so that different information is going to be displayed him as if he was part of the "tourists" group.
My question: What is the best way to realize these two user groups in my database design?
You should have UserCategories table:
CREATE TABLE `UserCategories` (
UC_ID INT PRIMARY KEY AUTO_INCREMENT,
IC_Title VARCHAR(100)
)engine=innodb;
Then in your users table have a column called CategoryID and have it reference UserCategories table on UC_ID = CategoryID.
This way you will know which category user belongs to whether its operator ID 1 or tourist ID 2.
The way I would do it is I would have another table call it Permissions that references Permission to UserCategories. That way you will be able to customize what kind of access Tourists should have.
Update:
SELECT COLUMN1, COLUMN2 ...
FROM users
INNER JOIN UserCategories ON UC_ID = CategoryID
INNER JOIN Permissions ON UC_ID = PCategoryID
WHERE Permission = 'update_data' OR Permission = 'delete_data'
Or you could grab all permissions for a specific user category and see if currently logged in user has that permission to perform that action.
An easy method of doing this would be to add a column to your USERS table which store the type of the group. For example: groupType could store either operator or tourist and depending on that value, you can show or hide stuff in your page.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to create a database that holds all my information with questions, users, answers etc.
I've been thinking all day how to solve this problem of mine. Okey, so the problem is like this:
Let's say I have a table in my DB, with questions (300++) and the user that logs on will get a random question shown. But never the same question again, so I'll need to store this information in a separate table with the user-ID and the question-ID. And I'll need to create another table that stores the answers to the questions.
So how would this PHP/MYSQL-code look like? Because I'll need to find a random question that hasnt been shown to the same user again.
If something is unclear, please let me know. And thanks in advance
You can use RAND() function and NOT IN
Example:
SELECT * FROM `questions` WHERE id NOT IN (5, 3) ORDER BY RAND() LIMIT 1;
You have to fetch question id using sub query
Example
SELECT * FROM `questions` WHERE id NOT IN (SELECT question_id FROM shown_questions WHERE user_id=1) ORDER BY RAND() LIMIT 1;
You can use an outer join to select items that have not been used already. It might be a bit faster than NOT IN if you set up the indexes:
SELECT questions.* FROM questions
LEFT JOIN shown_questions
ON (questions.id=show_questions.question AND user_id=42)
WHERE shown_questions.question IS NULL
ORDER BY RAND() LIMIT 1;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
can someone please help me with the below mysql query:
I want to build a query like:
UPDATE users
SET ref_id = (select max(ref_id)where `role_id`=5) +1
WHERE user_id =102
AND `role_id`=5
where your FROM clause in subquery???
UPDATE users
SET ref_id =
(select max(ref_id) FROM ???? where role_id=5) + 1
WHERE user_id =102 and role_id=5
Ok I tried a similar request and I had this MySQL error :
1093 - You can't specify target table 'my_table' for update in FROM clause
Try using the following request :
UPDATE `users`
SET
`ref_id` =
(
( SELECT max_id FROM ( SELECT MAX(ref_id) AS max_id FROM users WHERE `role_id`= 5) AS sub_selected_value )
+ 1
)
WHERE `user_id` = 102 AND `role_id` =5
Please keep in mind that may not be the most efficient solution if performance matters because it uses temporary tables.