Mysql delete query deletes only two columns in a row - php

I have a table with the following structure,
FIELD TYPE EXTRA
faciltiy_id int auto_increment(Primary Key)
hotel_id int
facility_title varchar(20)
facility_desc varchar(300)
When I want to delete a row with a particular facility_id I use the code,
DELETE
FROM $hotel_facilities
WHERE facilities_id = '$facilities_id'";
But instead of the whole row, only the facility_title and facility_desc fields are getting deleted. If I run this query directly through phpmyadmin over the table it works correctly.
Can anyone enlighten me on what i am doing wrong?

But instead of the whole row, only the facility_title and facility_desc fields are getting deleted. If I run this query directly through phpmyadmin over the table it works correctly...
Because the facilities_id is an INT, you don't need to enclose it within single quotes:
DELETE FROM {$hotel_facilities}
WHERE facilities_id = {$facilities_id}
But I think the real issue is that an UPDATE statement is being called, rather than the DELETE statement. Mainly because a DELETE statement only deletes entire rows, not specific columns.
You need to trace over what is being called when the submit is triggered, and print to screen (either using echo or via Javascript if it's more convenient) prior to execution.

Related

Updating and inserting a value in a database, locking its table and avoid multiple connection while working on the SELECT from php

I need to update and/or insert in a Postgres DB, a String value with letters and numbers, which must be incremented of +1 (through php strings functions) everytime there is an UPDATE.
I need to LOCK this table, in order for the php to complete its flow in inserting or updating it avoiding others that open the page to receive the same result of the SELECT.
The second arrived, would wait for the first to finish.
The second arrived, will ALWAYS make an UPDATE.
This update could generate a FILENAME0023, if there were 22 updates after the first insertion.
There could be more connections at the same time, I need to reserve the first result of the SELECT for the first one who connected to this php page.
The flow would be:
LOCK
SELECT column FROM table WHERE column = 'FILENAME0001';
IF NOT EXIST { INSERT INTO table (column) VALUES ($my_column); }
ELSE { UPDATE table SET column = '$my_new_column' WHERE column = '$my_column'; }
UNLOCK
The variable $my_new_column is a SUBSTR php function that would cut the number part of the string and will then be incremented of +1.
This link is helping me a lot: THIS.
But it does not contain everything.
I also tried working with stored procedure: LINK
But I should work on the php when it is an update because I cannot increment a DB value like shown HERE, because I do not have a INT but a string and I can not change this
Anyone who can help me?
I'd like to share my code, but believe me I'd rather start fresh, all the codes I tried are leading to nowhere.
you should try to use
`INSERT ON ON DUPLICATE KEY UPDATE`
Here link for more example about it

Trigger MSQL big issue, sql not working?

I am trying to build a trigger, the problems is it is not working. I have syntax problems. I need to build a trigger which will update a table with another record in the same table before the table has been been updated. The trigger below describes what i want to do but it does not work.
$deletequery = '
DELIMITER //
CREATE TRIGGER after_insert_job_sent
AFTER INSERT
ON Envato_CustomConfig_job_sent FOR EACH ROW
BEGIN
DELETE FROM `Envato_CustomConfig_Job_Queue`
WHERE Job_ID = '.$value['Job_ID'].'
AND email -'.$value['email'].';
UPDATE `envato_customconfig_job_status` SET `email_Sent_Count`= email_Sent_Count+1
WHERE Job_ID = '.$value['Job_ID'].'
END; //
DELIMITER;
insert into Envato_CustomConfig_job_sent
values ( NULL , '.$value['Job_ID'].', '.$value['email'].', now();
';
EDIT:
Why does my query append other records aswell?....
so basically it is working however on the update is add one to the last record the db. i have tried LIMIT it did not work. any ideas.
insert into Envato_CustomConfig_job_sent
values ( NULL , '37', 'email', now());
DELETE FROM Envato_CustomConfig_job_queue
WHERE Job_ID = '37'
AND email ='email';
UPDATE envato_customconfig_job_status SET `email_Sent_Count`= email_Sent_Count+1
WHERE Job_ID = '37';
Triggers, Events, Functions, and Stored Procedures reside as code stored in a particular database. Only some of them have parameter passing (such as Functions and Stored Procedures). Others just fire are their own (Events and Triggers).
What you are attempting to do is perform a delete with PHP variable information which is just a query.
At least you have the DELIMITER concept nailed down, that stands in the way as a trivial error that catches many people. But if you would have highlighted the Trigger block, you would have seen the syntax error highlighting, most likely, around the embedded WHERE Job_ID = '.$value['Job_ID'].' chunk. And since the trigger runs on triggered events such as insert, etc, it would have no clue about that chunk.
When I say highlighted, I mean in a program such as Mysql Workbench.

Duplicate entry '0' for key 'PRIMARY' in empty table

I am currently starting web development and am now working on a very simple script that posts entry data from a from to a mysql database. But the problem I have been encountering is that when I submit the form I get the following error:
Error: Duplicate entry '0' for key 'PRIMARY'
To me it seems a really weird error as the table is completely empty, ID is set to auto_increment and I am not trying to assign any value to it.
I am using Xampp for my localhost on Mac OS btw.
This is my form (.php):
This is my mysql entry script (.php):
The result is this:
This is the database setup:
The weird thing is that when the table is empty, and I insert through the form the first time, something does end up in the database. But it is missing "email" and "password" and shows "NULL". The second time I use the form, nothing is added in the database:
Your id column does not seem to be auto-incrementing. It is not generating a new id for every row. It just defaults to 0 if you don't supply a value for it.
Nonetheless there's a UNIQUE/PRIMARY constraint on that column, so the id 0 cannot occur more than once. Since you're not supplying an id and MySQL isn't generating one (because the column isn't auto-incrementing), you cannot insert more than one row.
You're issuing three separate INSERT INTO statements which will result in three separate rows to be inserted (if you could insert more than one row, see above), each with a different value set; but never one row with all values set.
So:
Make your id column actually auto-incrementing.
Prepare a single INSERT INTO statement which inserts all values in one go:
INSERT INTO members (name, email, password) VALUES (.., .., ..)
See Why shouldn't I use mysql_* functions in PHP? and stop using mysql_*. Learn about prepared statements, read The Great Escapism (Or: What You Need To Know To Work With Text Within Text) for why.

Why does a mysql Insert query fail *only* when the table is empty?

I am running these two statements one right after another in some PHP code.
INSERT INTO input (ID) VALUES('1');
UPDATE input SET `vendor_name` = 'some name' WHERE ID=1
If the database table is empty (meaning I just truncated the table), no data is created, no warnings appear. It's as if I never executed the query.
If I then run just
INSERT INTO input (ID) VALUES('1');
On this exact same clear table, an entry is created no problem.
After that, if I again run the same INSERT/UPDATE querys
INSERT INTO input (ID) VALUES('2');
UPDATE input SET `vendor_name` = 'some name' WHERE ID=2
Then the data is created and the vendor_name set appropriately. What is going on here?? I seem to be misunderstanding something fundamental about inserting data into a database. Can I not execute statements back-to-back like this on an empty table?
To pre-empt the inevitable silly questions like 'does the column exist?', here is some extra notes:
NO warnings ever appear. This really bugs me. The INSERT/UPDATE seems to fail silently. (yes, yes, I have set error reporting on and I checked the logs)
The appropriate columns/tables/databases/permissions exist and are defined (remember the query works fine if there is an empty row in the table)
ID is the primary key. The numbers I used for IDs (1,2, whatever) do not seem to matter. I could just as well reverse them, or use 101 and 102, whatever.
Create table looks like so: CREATE TABLE IF NOT EXISTS $tablename (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY). Columns are added dynamically by users. So right now there's about 100 columns in the table ( keep in mind this shouldn't matter. I'm only trying to update one column, and that update works fine if there is data in the table)
Sneaking a Count(*) query in there reveals that the row is created, but then disappears after the Update statement. (Perhaps the row wasn't completed, or something, and there's a 'make sure the insert is finished' query I need to run?)
Try this code.... for me its works....
INSERT INTO `input` (ID) VALUES('1');
UPDATE `input` SET `vendor_name` = 'some name' WHERE ID=1

ID cannot be null (Auto Increment)

I'm using an INSERT ON DUPLICATE KEY statement for my website. It's for creating news items, so I figured I could use the same MySQL command for both creating and updating news items.
However, when I use the following:
INSERT INTO table (id,title,content) VALUES(NULL,"Test","Test");
Instead of creating a new auto increment value it throws an error. However, the command works on my main development server. But not on my laptop. Both versions of MySQL are the same, the only difference being MySQL was installed manually on my server, and with WAMP on my laptop.
Are there any MySQL Variables that could be causing this?
I would suggest using INSERT INTO table (title,content) VALUES("Test","Test");
This will create a new row in the table with a new incremented ID.
Managed to solve it as best as I can.
I checked my code and found that when I inserted the empty POST'd ID was wrapping it in quotations. I've now changed it so that it puts NULL without quotations. So my query should now look like:
INSERT INTO table (id,title,content) VALUES(NULL,"test","Test")
ON DUPLICATE KEY UPDATE title=VALUES(title), content=VALUES(content);
That now works.
I think you should make query like this,
INSERT INTO table (title,content) VALUES("Test","Test");
If it still doesn't work then check if id column is set as auto-increment or not.

Categories