MySQL CREATE TRIGGER using php [duplicate] - php

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I have read some other posts like mine, but none of them solve my problem,
I have two tables, user and purchase, When a user signup, I want to add the same user_id to the purchase table as well. I try to right a php code for that like this:
$query = "CREATE TRIGGER `purchase_insert` AFTER INSERT ON `user` FOR EACH ROW BEGIN INSERT INTO purchase (user_id) VALUES (NEW.user_id)";
$result = mysqli_query($connection, $query);
if($result){echo "<br>TRIGGER Success!";} else {die("<br>Database query failed. " . mysqli_error($connection));}
this is the error I get:
Database query failed. 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 '' at line 1
I'm sorry if the question is repetitious.

It should be back tick not single quotes
CREATE TRIGGER `purchase_insert` AFTER INSERT ON `user` FOR EACH ROW BEGIN INSERT INTO purchase (user_id) VALUES (NEW.user_id)
UPDATE
Try this
DELIMITER$$
CREATE TRIGGER `purchase_insert`
AFTER INSERT ON `user`
FOR EACH ROW
BEGIN
INSERT INTO purchase (user_id) VALUES (NEW.user_id);
END$$
DELIMITER ;

Related

Is there any other way to delete rows from multiple tables without creating foreign key between tables? [duplicate]

This question already has answers here:
How to delete from multiple tables in MySQL?
(7 answers)
Closed 2 years ago.
This code does not work. Without creating foreign key between tables how can I delete data from multiple tables that matches the conditions? How can I write a 2 query and send it with PHP-PDO
$query = "DELETE FROM category, bookmark WHERE (bookmark.category = ? AND category.name = ?)";
$stmt = $db->prepare($query);
$stmt->execute([$categoryName, $categoryName]);
I am using "?" to prevent from sql injection.
This is the error I get.
JavaSQLSTATE[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 'WHERE
(bookmark.category = 'Java' AND category.name = 'Java')' at line 1
In multi-table DELETE you MUST specify the tables which records must be deleted:
DELETE category, bookmark
FROM category, bookmark
WHERE (bookmark.category = ? AND category.name = ?)
See MySQL 8.0 Reference Manual / ... / DELETE Statement, section "Multiple-Table Syntax". The names of tables which records must be deleted are NOT enclosed into square brackets as optonal (2nd row of query text in both syntax variants).

MySQL "IF NOT EXISTS" [duplicate]

This question already has answers here:
MySQL: Insert record if not exists in table [duplicate]
(16 answers)
Closed 5 years ago.
I am having error in SQL syntax but I don't see it.
$query = "IF NOT EXISTS ( SELECT id FROM Provider
WHERE name=('$filename') )
INSERT INTO Provider (Name) VALUES ('$filename')";
$query_result = mysqli_query($connect, $query);
The code
INSERT INTO Provider (Name) VALUES ('$filename')
worked normally if I use just that.
Also the code
SELECT id FROM Provider WHERE name=('$filename')
worked fine when I tested its value
When I added IF NOT EXISTS I have SQL syntax error but I can't see it!
Any ideas?
You cannot use NOT EXISTS in this context. Try the following query instead:
INSERT INTO Provider (Name)
SELECT filename
FROM (SELECT ('$filename') AS filename) AS t
WHERE NOT EXISTS (SELECT id
FROM Provider
WHERE name=('$filename')

MysQL SELECT statement if empty [duplicate]

This question already has answers here:
How can I do 'insert if not exists' in MySQL?
(11 answers)
Closed 5 years ago.
I am struggling a bit with something that I think should be simple to solve.
I first check the database to see if it exists. I would like it to insert the date if it does not exist but I am not sure how to structure the IF statement part. Many thanks
$date='2017-05-13';
$select_date = mysqli_query($link, "SELECT * from `marker` WHERE `date`='$date' ");
$insert_date = mysqli_query($link, "INSERT INTO `marker` (`date`,`value`) VALUES ('$date','1') ");
In general, for this type of operation, you want to use on duplicate key update. This starts with a unique index:
CREATE UNIQUE INDEX unq_marker_date ON marker(date);
Then the database guarantees only one row per date. You can then do the insert as:
INSERT INTO `marker` (`date`, `value`)
VALUES ('$date', '1')
ON DUPLICATE KEY UPDATE `date` = VALUES(`date`);
The ON DUPLICATE KEY part does nothing except prevent an error for a duplicate date.

PHP - Insert SQL Error [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 6 years ago.
Database layout:
rid (auto increment) (primary key) (255)
song (varchar) (120)
artist (varchar) (30)
by(varchar) (33)
key(varchar) (60)
PHP code:
$sql = "INSERT INTO Requests (song,artist,by,key)
VALUES ('$song','$artist','$by','$key')";
if($this->db->query($sql))
{
die("true");
}
echo 'false: ' . $this->db->error;
Error:
false: 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 'by,key) VALUES ('testsong','testing','kyle','example')' at line 1
Help? I have debuged for ages, I can't see whats wrong with that SQL? Thanks in advance!
You need to use backtick for BY and KEY columns, both are mysql reserve words
$sql = "INSERT INTO Requests (`song`,`artist`,`by`,`key`)
VALUES ('$song','$artist','$by','$key')";
MYSQL Reserve Words List
Side Note:
I suggest you that, please do not use reserve words and keywords for table or column names.
You'll need back ticks for SQL-related names, also by and key are MySQL reserved words:
INSERT INTO Requests (`song`,`artist`,`by`,`key`)
Try this query to insert data in Requests table
$sql = "INSERT INTO Requests (`song`,`artist`,`by`,`key`)
VALUES ('".$song."','".$artist."','".$by."','".$key."')";
if ($this->db->query($sql)) {
die("true");
}
echo 'false: ' . $this->db->error;
the main problem in the asked question's query is apostrophe on both side of column name is missed as (song, artist, by, key) Should be ('song', 'artist', 'by', key')

What faster "Insert ignore" or "Select and Insert" [duplicate]

This question already has answers here:
Which DB design is faster: a unique index and INSERT IGNORE, or using SELECT to find existing records?
(6 answers)
Closed 9 years ago.
mysql database.
Table have index on field "Code".
I need to insert to table new rows.
What works faster?
1)
simple index on field Code - for fast select
befor insert check rows : SELECT COUNT(*) FROM table WHERE Code = 'NewCode';
simple insert(if rows not found): Insert into table values ('NewCode')
2)
unique index on field Code - for insert
Insert IGNORE into table values ('NewCode')
For me more secure (and can be a backup of changes - better way) is the first, but I think that the action is similar.
Consult http://dev.mysql.com/doc/refman/5.5/en/insert.html for more detali
paladinux

Categories