MySQL NOT EXITS condition not working - php

Hello I am trying to execute a mysql query but getting error which I can't understand. The query I am using is
INSERT INTO summary (oid,tab,cost) VALUES('1','7','40') WHERE NOT EXISTS (SELECT * FROM summary WHERE cusid ='1')
I am using this tutorial as my reference, http://www.techonthenet.com/mysql/exists.php
This is the structure of the summary table
This is the error message
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use new 'WHERE NOT EXISTS (SELECT * FROM summary WHERE cusid = 1)' at line 1

It is not exits. Change it to exists.
INSERT INTO summary (oid,tab,cost) VALUES('1','7','40') WHERE NOT EXISTS (SELECT * FROM summary WHERE cusid ='1')
^
Also you can't use NOT EXISTS with your above query, you can with a subquery.

Related

Thread Options MariaDB Sql Injection syntax to use near

I have tried sql injection my script. I have a problem in ezSQL.
Original query
$dbo->get_var("SELECT COUNT(*) FROM table WHERE id = '1'");
Injected Query
$dbo->get_var("SELECT COUNT(*) FROM table WHERE id = '1'; SELECT * FROM table -- -'");
Error
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 'SELECT * FROM table -- -' at line 1
But,
This Sql Query works phpmyadmin SQL Command successfully. I dont understand this. Why sql code doesnt work in ezsql query?
Please help me.

Error with not exists code - mariaDB

I'm not sure why:
INSERT INTO $db.further_assess (taskid) VALUES ('id')
WHERE NOT EXISTS (SELECT * FROM $db.further_assess where taskid='$id')
is giving me this error:
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 'WHERE NOT
EXISTS (SELECT 1 FROM risk_assessment.further_assess where taskid='222' at line 2
was following this: Sql insert if row does not exist
Update:
My, now correct, query:
INSERT INTO $db.further_assess (taskid, reportid)
SELECT '$id', '$report_id'
FROM (SELECT 1) as dummytable
WHERE NOT EXISTS (SELECT * FROM $db.further_assess where taskid='$id');
An INSERT statement has no WHERE clause. If you want to run a conditional INSERT statement you can use an INSERT-SELECT statement with a dummy table:
INSERT INTO $db.further_assess (taskid)
SELECT 'id'
FROM (SELECT 1) as dummytable
WHERE NOT EXISTS (SELECT * FROM $db.further_assess where taskid='$id')
Much simpler (and faster) to do
INSERT IGNORE INTO $db.further_assess
(taskid)
VALUES
('$id')

Why is this SQL incorrect (MySQL)?

I have a following query:
"DELETE * FROM Participations WHERE scheduleId IN (SELECT id FROM Schedule WHERE meetingId = :meetingId) AND userId = :userId"
I assign variables using PDO properly.
In response I get:
SQLSTATE[42000]: Syntax error or access violation: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM Participations WHERE scheduleId IN (SELECT id FROM Schedule WHERE meeting' at line 1}
It is enough that I replace first DELETE with SELECT and query is done properly.
Besides when I test directly in phpMyAdmin the same query with DELETE in it works fine.
I am confused....
Use DELETE FROM (note the lack of *) instead.
The error message clearly indicates that it is a syntax error near '*'.
Remove * from your Delete statement
DELETE FROM Participations WHERE scheduleId IN
(SELECT id FROM Schedule WHERE meetingId = :meetingId) AND userId = :userId

Use of CONCAT in FROM clause

I have a non-working query as the sub-query FROM clause doesn't understand CONCAT():
SELECT *
FROM `events` e
WHERE EXISTS (SELECT *
FROM CONCAT('prefix_', e.`event_id`) registrations
WHERE registrations.`attendee` = 123456
)
Is there any way to make this work in a single-statement?
The error message I receive is:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('prefix_', e.event_id) registrations
You are trying to dynamically come up with a table name? I don't think this is possible...because doesn't the FROM clause table name need to be resolved prior to the SQL being able to be evaluated and executed?
SELECT * FROM (SELECT *,
CONCAT('prefix_', `event_id`) as prefix FROM `events`
WHERE events.attendee = 123456
);

SQL/PHP query error

I have the following error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select manufacturers_id, manufacturers_name, manufacturers_image, date_added, la' at line 1
The query:
SELECT COUNT(
SELECT manufacturers_id,
manufacturers_name,
manufacturers_image,
date_added,
last_modified
FROM manufacturers
ORDER BY manufacturers_name
) AS total
This happens also to some other categories in my website. All finish their line incomplete.
This query would do what you want:
SELECT COUNT(*) AS total
FROM manufacturers
What's wrong with your query:
COUNT() accepts an expression or single column as parameter - you're passing multiple
ORDER BY doesn't make much sense as soon as what you need is just number of rows (for obvious reason that number doesn't depend on order)
Maybe because the code is not on here full but what I can see is the mysql/mysqli_query is missing, and the ";" at the end of the code.

Categories