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.
Related
I am new with PHP development and just wondering if theres a existing function on PHP than duplicate the copy command on phpmyadmin, i know that the query sequence is below, but this is like a long query/code since the table has alot of columns. i mean if phpmyadmin has this feature maybe its calling a build in function?
SELECT * FROM table where id = X
INSERT INTO table (XXX)VALUES(XXX)
Where the information is based from the SELECT query
Note: The id is primary and auto increment.
Here is the copy command on phpmyadmin
i mean if phpmyadmin has this feature maybe its calling a build in function?
There is no built-in functionality in MySQL to duplicate a row other than an INSERT statement of the form: INSERT INTO tableName ( columns-specification ) SELECT columns-specification FROM tableName WHERE primaryKeyColumns = primaryKeyValue.
The problem is you need to know the names of the columns beforehand, you also need to exclude auto_increment columns, as well as primary-key columns, and know how to come up with "smart defaults" for non-auto_increment primary key columns, especially composite keys. You'll also need to consider if any triggers should be executed too - and how to handle any constraints and indexes that may be designed to prevent duplicate values that a "copy" operation might introduce.
You can still do it in PHP, or even pure-MySQL (inside a sproc, using Dynamic SQL) but you'll need to query information_schema to get metadata about your database - which may be more trouble than it's worth.
I am not fluent in using phpmyadmin so please be gentle.
I have transferred wordpress sites in the past but they have small databases. Ive now moved a new clients site to my hosting and it is running with this error:
INSERT INTO `wp_options` VALUES ( 81057,
'_wc_session_6f1ee0a5a9d89e47f7941c9e3b3e1fed',
'a:20:{s:4:"cart";s:309:"a:1:{s:32:"7b1ce3d73b70f1a7246e7b76a35fb552";a:9:{s:10:"product_id";i:2103;s:12:"variation_id";s:0:"";s:9:"variation";s:0:"";s:8:"quantity";i:1;s:10:"line_total";d:23;s:8:"line_tax";i:0;s:13:"line_subtotal";d:23;s:17:"line_subtotal_tax";i:0;s:13:"line_tax_data";a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}}}";s:15:"applied_coupons";s:6:"a:0:{}";s:23:"coupon_discount_amounts";s:6:"a:0:{}";s:19:"cart_contents_total";d:23;s:20:"cart_contents_weight";i:0;s:19:"cart_contents_count";i:1;s:17:"cart_contents_tax";i:0;s:5:"total";i:0;s:8:"subtotal";d:23;s:15:"subtotal_ex_tax";d:23;s:9:"tax_total";i:0;s:5:"taxes";s:6:"a:0:{}";s:14:"shipping_taxes";s:6:"a:0:{}";s:13:"discount_cart";i:0;s:14:"discount_total";i:0;s:14:"shipping_total";i:0;s:18:"shipping_tax_total";i:0;s:9:"fee_total";i:0;s:4:"fees";s:6:"a:0:{}";s:10:"wc_notices";N;}',
no' ) ;`
#1062 - Duplicate entry '81057' for key 'PRIMARY'
I genuinely do not have a clue what this means and how to change it. I get that there is a duplicate entry and its to do with the primary key - which is set on option_id.
What i don't get is why it doesn't just add the entries into the database and auto increment them - which it is set to do? Also how can i resolve the issue and add the database?
please help !
AUTO_INCREMENT is a default value. If you specify a value for the field it will be used instead.
You can omit the value in the insert statement or use NULL instead.
It looks like the table in your source database has some inconsistency since you are only trying to copy all records to your new database.
Check if there are multiple records with same option_id in your source table and if there are some try to resolve them and then try the import again.
As i understand you are trying to do the manual transfer of the whole database and in that case AUTO_INCREMENT just wont work since you will break the relations between your model entities.
The way I see it, you have two options.
Omit the primary key value and allow mysql to create a new row and auto increment it. Of course this will require you to specify the columns.
INSERT INTO `wp_options`(column2, column3, etc)
VALUES('_wc_session_6f1ee0a5a9d89e47f7941c9e3b3e1fed', ...);
Notice that I left out the primary key
If the data needs to be added to that specific row, do an UPDATE instead.
UPDATE wp_options SET column2='_wc_session_6f1ee0a5a9d89e47f7941c9e3b3e1fed',
column3='something else', ...
WHERE column1=81057
OK, so here is my scenario - you may disagree with what I'm attempting to do but I have my reasons.
The user is able to upload various information to the database, I also want them to be able to upload a picture at the same time. Now for various reasons I want to store the image with the file name the ID number of the record I'm adding (I have an ID column as the primary which auto-increments). Obviously I would have to store the file extension in the database too.
Now is there a way I can add the record and then know what ID was set so that I can save the image? I don't want to query and select the highest ID as that could go wrong if two people were to submit the form at the same time.
Any ideas?
LAST_INSERT_ID() will give you the ID that was auto-generated:
... returns a BIGINT (64-bit) value representing the first
automatically generated value that was set for an AUTO_INCREMENT
column by the most recently executed INSERT statement to affect such
a column. For example, after inserting a row that generates an
AUTO_INCREMENT value, you can get the value like this:
mysql> SELECT LAST_INSERT_ID();
-> 195
After running an insert in PHP, you can get the AUTO_INCREMENT ID from $mysqli->insert_id (where $mysqli is a MySQLi connection object), $pdo->lastInsertId (where $pdo is a PDO connection object), or mysql_insert_id() (if you're still using MySQL, which you shouldn't be) without having to run another query.
Let's say I have a MySQL table and a table has a row with id and it has auto_incremented. Let's say via MySQL query and PHP, I add a row. The first row has id of 1. Then I manually add a second row (via phpmyadmin) with the id of 2. If I do a third MySQL insert via PHP... what would the id be for the third row... 2 or 3?
Question is... does auto_increment take into account manual inputs?
does auto_increment take into account manual inputs?
Yes it does. But I hope you do not really type in the ID manually, right? :-) Just leave this field alone when inserting (manually or programatically), MySQL will take care of it for you.
MySQL does accept manual inputs, and it WILL try to set the value you offer. If the value does not exist, it gets inserted, else you get a duplicate key error.
Put a value when you want to decide a value yourself (for example,
you deleted a line, and now want the exact same line in the table).
Put NULL or leave the column out of the insert to let the database
use the auto-increment.
Just a hint: when your application is choosing the values to put for an autoincrement value, you are probably doing something wrong.
I am currently working on a PHP project with an Oracle database. To update a table, the php code I'm working with uses a SQL "MERGE INTO" method to loop through a table and see if values for multiple records exist in another table. If they don't exist yet, the values are inserted into my table. If the values already exist, nothing happens.
I would like to have another query run after this that uses the auto incremented id's created in the MERGE INTO query. Is there a way to get an array of the newly created ids? I was hoping for something like mysql_insert_id, but I haven't found anything like that yet.
Thanks!
Oracle has supported the MERGE syntax since 9i. Haven't tried, but you might be able to use the RETURNING clause on the MERGE statement...
Oracle uses sequences for handling automatically incremented values. Once you've created a sequence, you can use:
sequence_name.CURVAL
..to get the current value, like what mysql_insert_id would return. To populate a primary key, you'd use:
sequence_name.NEXTVAL
To populate a primary in an INSERT statement, you'd use:
INSERT INTO your_table
(pk_id, ..
VALUES
(your_sequence.NEXTVAL, ...)
You can use triggers as an alternative, but they won't return the current value.
What auto_incremented ids? AFAIK, There is no such thing in Oracle. You can simulate the behaviour by adding a trigger on the table and a sequence number but there is certainly no equivalent of mysql_insert_id().
I think you need to go back and find another way to identify your records.
C.