How can i delete table using alises on sql query? [closed] - php

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.

Related

How to drop a table with register form (SQL Injection) (closed) [duplicate]

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 6 years ago.
Improve this question
I am trying to fetch bulk data from a website database but could not succeed. Can somebody suggest if SQL injection is possible and how to do in this case.
There are many ways to do SQL Injection to a website similar to the one you provided.
In the where clause it is expecting ac_no. I assume that this value is being passed from the browser as user input. In that case you can pass ac_no value along with or 1 = 1. e.g where ac_no = 123 or 1 = 1. It returns everything from the table RollPdf1.
For string comparison you can add "" = "" to the where clause.
If you want to perform other select operations ( if you know other table names) then you can append select statements delmited by ;.
UNION operator :
If you know the data types of the columns selected in the query then you can use UNION to get additional data from other tables.
e.g
original query : select name, age, sex from table1 where id = 1
sql injected query : select name, age, sex from table1 where id = 1 AND 1 = 2 UNION select username, id, password from userstable or someother table.

Select from database without duplicate [closed]

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 6 years ago.
Improve this question
I'm trying to develop php page but I have a problem, I would like to get data from data base without duplicate.
$tsql = "SELECT COUNT(ID) FROM FactoriesViolations";
$rowsPerPage = 25;
$stmt = sqlsrv_query($conn, $tsql);
please help me.
thanks in advance.
what all columns are you expecting in your output. If its only ID
$tsql = "SELECT COUNT(DISTINCT(ID)) FROM FactoriesViolations";
if you want all the columns from the table and eliminate the duplicate records
this query will do the needful.
SELECT Col1, Col2,... ColN FROM FactoriesViolations GROUP BY Col1, Col2,... ColN;
here Col1, Col2,... ColN are column names of your FactoriesViolations table.
Use below query. It will work.
$tsql = "SELECT COUNT(DISTINCT ID) FROM FactoriesViolations";
use below way for count of unique records
SELECT COUNT(DISTINCT column_name) FROM FactoriesViolations; // column_name is column which contains duplicate
DISTINCT keyword tells the server to go through the whole result set and remove all duplicate rows after the query has been performed.
Format :
SELECT DISTINCT *
FROM TABLE_NAME
WHERE CONDITION(S)
In your case, the following query should work
$tsql = "SELECT COUNT(DISTINCT(ID)) FROM FactoriesViolations" ;
This will return the count of all unique IDs existing in the table.

Delete from mysql using joins? [closed]

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

How to update data after insert? [closed]

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
mysql_query("INSERT INTO withdraw (username,amount,date) SELECT username,amount,NOW() FROM accounts WHERE username='$username' UPDATE accounts SET amount =0 WHERE username = '$user'")
i want to update data after insert but this query not working
Just put all the columns you need to insert in the select-part
INSERT INTO withdraw (username, amount, some_date)
SELECT username, amount, now()
FROM accounts
WHERE username='$username';
This is how the syntax should be -
INSERT INTO Table2
(client_last_name, client_first_name, taskDescription, category)
(SELECT clientLastName, clientFirstName, incidentDescription, category
FROM Table1
WHERE info_field IS NOT NULL)
ADVICE: Avoid using mysql_* functions as they are deprecated in recent PHP versions. Learn mysqli prepared statement or PDO and start implementing.
In my opinion, you can split your statement into two parts. Assuming that you want to make NOW() to be current date in all inserted rows (I write as in SQL Server because I can't write in MySQL, if you can please try to convert it into MySQL):
#date=NOW();
INSERT INTO withdraw (username,amount) SELECT username,amount FROM account WHERE
username=#username;
UPDATE withdraw
SET date=#DATE where ID=##IDENTITY
If your table has IDENTITY ID column. In MySQL it is auto increment and an appropriate function is SCOPE_IDENTITY() (selects last inserted ID in MySQL, you can google for more information)

Multiple ID SQL query [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let's just say I have two tables.
The first one :
id thing1 thing2
The second :
id_fo other_thing
where id_fo depend from the first one table.
I have to insert thingS in both table, but in php mysql request (pdo for example), how can I get all last insert id ?
I mean, if I had 30 row in one query, I will have 30 new id.
How can I do my second SQL query ?
In Psuedo code:
Start transaction
insert into table1 (thing1, thing2) values (thingS,thingS2)
lastidtable1 = lastinsertedid();
insert into table2 (id_fo, other_thing) values (lastidtable1,thingS);
lastidtable2 = lastinsertedId();
commit;
Every of the above lines should php code which calls a query.
Are you looking for something like this ?
$main_data = array('dino', 'babu', 'john');
foreach($main_data as $main) {
// Insert main to 1st table
mysql_query("MY INSERT QUERY TO TABLE 1");
// Get the last insert id
$new_id = mysql_insert_id();
// Insert the sub data to 2nd table using the insert id
mysql_query("MY INSERT QUERY TO TABLE 2 USING $new_id ");
}
INSERT and UPDATE do not return any datasets,you can add an INSERTED column with timestamp type,set DEFAULT to CURRENT_TIMESTAMP and select all rows with the greatest timestamp value.
if you can guarantee that there is only one insert process going on at a time you could do something like this psuedo code:
$max1=select max(id) as max1 from table;
insert into table ....
$num_inserted_rows=number of inserted rows.
$max2=select max(id) as max2 from table;
if(($max2-$max1) == $num_inserted_rows){
$lastid=select last_insert_id();
$inserted_ids=range($lastid-$num_inserted_rows,$lastid);
}

Categories