How to update data after insert? [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 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)

Related

How can i delete table using alises on sql query? [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 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.

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.

Select entire table contents and insert into another table with the same column names [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 8 years ago.
Improve this question
I have two tables that have the same column names. Is there a way of getting the contents from one table and inserting it into another. I could export it into a sreadsheet then import it into the other table but would prefer to quickly use mysql. My experience with php and mysql is mainly selecting and inserting from one table.
Use INSERT ... SELECT:
INSERT INTO table1 SELECT * FROM table2
Note that this will only work if the tables are defined with columns in the same order; otherwise one will have to explicitly name the columns in one (or both) of the INSERT and SELECT parts of the command:
INSERT INTO table1 (colA, colB, colC) SELECT colA, colB, colC FROM table2

Php, how to copy a table from a database and paste to another [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I need to retrieve a table and all his fields from a database A and after, i need to paste this table on an another database B.
Theses 2 tables are on the same server, and i need to do that from a PHP script on the server B.
Do you have any ideas ?
Thanks !
Use the following SQL:
CREATE TABLE db2.table LIKE db1.table;
INSERT INTO db2.table SELECT * FROM db1.table;
If you copy the whole table with data and structure (as you do) , you can even simplify this into one single command like this:
CREATE TABLE db1.table
AS (SELECT * FROM db2.table);
INSERT INTO destTable
SELECT Field1,Field2,Field3,...
FROM srcTable
WHERE NOT EXISTS(SELECT *
FROM destTable
WHERE (srcTable.Field1=destTable.Field1 and
SrcTable.Field2=DestTable.Field2...etc.)
)
With various PHP extensions:
*_query/execute('insert into table2 select * from table1');
inserting all select values

Categories