UPDATE table one... and INSERT into table two - mysql

When I try to UPDATE and INSERT into two different tables on the same user action, it has an error where it only completes the command of either the insert or the update depending on which line comes last.
Is there a way to combine these two ?
$sql = "INSERT INTO photos(user, gallery, filename, uploaddate)
VALUES ('$log_username','profile pictures','$db_file_name',now())";
$sql = "UPDATE users SET avatar='$db_file_name'
WHERE username='$log_username' LIMIT 1";

Have you thought about using a trigger on the photos table? You could setup a tigger to execute every time an insert occurs that would update the users table.
Here's a link to check out:
http://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx

1) Execute the first query and then execute the second query.
2) use transaction if mysql if want want the successful execution of both the queries otherwise transaction control mechanism will revert the change.
And what error are you getting when you are firing the queries one by one

Related

Combining 2 Queries with OR operator

I'm trying to insert something to a table and also delete at the same time so my query is like this
$query = mysqli_query($connect,"SELECT * FROM inventory_item WHERE status = 'Unserviceable' OR DELETE * FROM inventory_item WHERE status = 'Available")
or die ("Error: Could not fetch rows!");
$count = 0;
I wanted to insert datas with Unserviceable status and at the same time delete datas with Available status but its not working.
I'm not really familiar with queries and just starting out.
This is not valid SQL syntax.
If you want to issue two queries, one to INSERT and one to DELETE, then you can send them as two separate calls to mysqli_query(). There appears to be an alternate function mysqli_multi_query() that allows multiple statements to be included which you can read about here.
Finally, if you want the two separate queries to execute as a single unit (that is, if one of them fails then neither is executed) then you should research the subject of database transactions, which allow you to execute multiple queries and commit or roll back the entire set of queries as a unit.

I need to find out who last updated a mysql database

I can get a last update time from TABLES in information_schema. Can I get a USER who updated the database or a table?
As Amadan mentioned, I'm pretty sure there isn't a way to do this unless you record it yourself. However, this is a pretty straightforward thing to do: Whenever you perform an UPDATE query, also log in a separate table the user (as well as any other relevant information) that you want to record via an additional MySQL query. Something like this (written in PHP as you didn't specify a language, but the MySQL can be exported anywhere) will work:
// The update query
$stmt = $db->prepare("UPDATE table SET `col` = ? WHERE `col` = ?");
$stmt->execute(array($var1, $var2));
// Something in table has just been updated; record user's id and time of update
$stmt = $db->prepare("INSERT INTO log (userid, `time`) VALUES (?, NOW())");
$stmt->execute(array($userid));

MySQL - Split up INSERT in to 2 queries maybe

I have an INSERT query which looks like:
$db->Query("INSERT INTO `surfed` (user, site) VALUES('".$data['id']."', '".$id."')");
Basically I want to insert just like the above query but if the site is already submitted by another user I don't want it to then re-submit the same $id in to the site column. But multiple users can view the same site and all users need to be in the same row as the site that they have viewed which causes the surfed table to have 10s of thousands of inserts which dramatically slows down the site.
Is there any way to maybe split up the insert in some way so that if a site is already submitted it won't then submit it again for another user. Maybe there's a way to use UPDATE so that there isn't an overload of inserts?
Thanks,
I guess the easiest way to do it would be setting up a stored procedure which executes a SELECT to check if the user-site-combination is already in the table. If not, you execute the insert statement. If that combination already exist, you're done and don't execute the insert.
Check out the manual on stored procedures
http://dev.mysql.com/doc/refman/5.1/en/create-procedure.html
You need to set a conditional statement that asks whether the id already exists then if it does update otherwise insert
If you don't need to know whether you actually inserted a line, you can use INSERT IGNORE ....
$db->Query("INSERT IGNORE INTO `surfed` (user, site) VALUES('".$data['id']."', '".$id."')");
But this assumes that you have a unique key defined for the columns.
IGNORE here will ignore the Integrity constraint violation error triggered by attempting to add the same unique key twice.
The MySQL Reference Manual on the INSERT syntax has some informations on that http://dev.mysql.com/doc/refman/5.5/en/insert.html

Replace a sql server table with a new one

I want to replace a table "Met" in my sql server database with a new datatable from an application.
My basic idea to loop each row in the new table and compare the existing table.
I used a stored procedure, but it only having "insert "and "update" function. Do I need delete the old table first?
Thanks
For each row, I want to loop the following stored procedure.
;WITH CTE AS (SELECT skey=#skey,ProbMetID=#ProbMetID,Interval=#Interval,Counts=#Counts)
MERGE Met AS TARGET
USING CTE SOURCE
ON SOURCE.skey = TARGET.skey
WHEN MATCHED THEN
UPDATE
SET ProbMetID = SOURCE.ProbMetID,
Interval = SOURCE.Interval,
Counts = SOURCE.Counts,
WHERE skey = #skey
WHEN NOT MATCHED THEN
INSERT INTO Met(skey,ProbMetID,Interval,Counts)
VALUES(#skey,#ProbMetID,#Interval,#Counts);
The MERGE statement does include a DELETE function. Check out Pinal Dave's post on it: http://blog.sqlauthority.com/2008/08/28/sql-server-2008-introduction-to-merge-statement-one-statement-for-insert-update-delete/
I've found that the easiest way to do this from the app layer is to insert everything into a temp table, then perform the entire MERGE operation between the temp table and the actual table. The great part is that you can do the entire thing in a single transaction. Plus, you can bulk insert the application table into the temp table for super awesome db speed (TM).

Capturing MySQL Statement and inserting it into another db via trigger

I want to be able to keep a log of every sql statment that edits a specific table, and I think the best way would be with a trigger. However, I don't know how to get the statement that triggers the trigger.
For example if I run:
$sql = "INSERT INTO foo (col_1) VALUES ('bar');"
I want to be able to craft an insert statement like:
$sql = "INSERT INTO log (statements) VALUES (INSERT INTO foo (col_1) VALUES ('bar')'
Is this possible with a trigger? I know I can do this easily with php, but I figured that a trigger would have less overhead.
You can't get the statements, only their results / alterations. Consider using a database abstraction- or decorator-interface to capture those queries.