What I'm trying to do is INSERT subscribers in my database, but IF EXISTS it should UPDATE the row, ELSE INSERT INTO a new row.
Ofcourse I connect to the database first and GET the $name, $email and $birthday from the url string.
$con=mysqli_connect("localhost","---","---","---");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$name=$_GET['name'];
$email=$_GET['email'];
$birthday=$_GET['birthday'];
This works, but just adds the new row;
mysqli_query($con,"INSERT INTO subs (subs_name, subs_email, subs_birthday)
VALUES ('$name', '$email', '$birthday')");
mysqli_close($con);
Here's what I tried;
mysqli_query($con,"INSERT INTO subs (subs_name, subs_email, subs_birthday)
VALUES '$name', '$email', '$birthday'
ON DUPLICATE KEY UPDATE subs_name = VALUES($name), subs_birthday = VALUES($birthday)");
mysqli_close($con);
and
mysqli_query($con,"IF EXISTS (SELECT * FROM subs WHERE subs_email='$email')
UPDATE subs SET subs_name='$name', subs_birthday='$birthday' WHERE subs_email='$email'
ELSE
INSERT INTO subs (subs_name, subs_email, subs_birthday) VALUES ('$name', '$email', '$birthday')");
mysqli_close($con);
and
mysqli_query($con,"IF NOT EXISTS(SELECT * FROM subs WHERE subs_email='$email')
Begin
INSERT INTO subs (subs_name, subs_email, subs_birthday)
VALUES ('$name', '$email', '$birthday')
End");
mysqli_close($con);
But none of them work, what am I doing wrong?
Any help is greatly appreciated!
Create a UNIQUE constraint on your subs_email column, if one does not already exist:
ALTER TABLE subs ADD UNIQUE (subs_email)
Use INSERT ... ON DUPLICATE KEY UPDATE:
INSERT INTO subs
(subs_name, subs_email, subs_birthday)
VALUES
(?, ?, ?)
ON DUPLICATE KEY UPDATE
subs_name = VALUES(subs_name),
subs_birthday = VALUES(subs_birthday)
You can use the VALUES(col_name) function in the UPDATE clause to
refer to column values from the INSERT portion of the INSERT ... ON
DUPLICATE KEY UPDATE - dev.mysql.com
Note that I have used parameter placeholders in the place of string literals, as one really should be using parameterised statements to defend against SQL injection attacks.
Try this:
INSERT INTO `center_course_fee` (`fk_course_id`,`fk_center_code`,`course_fee`) VALUES ('69', '4920153', '6000') ON DUPLICATE KEY UPDATE `course_fee` = '6000';
INSERT ... ON DUPLICATE KEY UPDATE
is a good solution as long as you don't mind AUTO_INCREMENT counters unnecessarily incrementing every time you end up doing an UPDATE. Since it tries to INSERT first, I noticed auto counters do increment.
Another solution I like that may be less performant, but easy to maintain is:
IF EXISTS(SELECT 1 FROM table WHERE column = value...) THEN
UPDATE table
SET column = value ...
WHERE other_column = other_value ...;
ELSE
INSERT INTO table
(column1, column2, ...)
VALUES
(value1, value2, ...);
END IF;
Related
Please Help me,
How can I insert data from one table to another table after insert a data in first table?
I wrote a query but it has problem, It said "Unknown column 'test.ProjectNumber' ". should I define it first?
I want insert data to test.ProjectNumber from test2.PN
Please help me
Code:
/**
* Insert a record on another table
*/
// SQL statement parameters
$insert_table = 'test2';
$insert_fields = array(
'test2.PN' => 'test.ProjectNumber',
);
// Insert record
$insert_sql = 'INSERT INTO ' . $insert_table
. ' (' . implode(', ', array_keys($insert_fields)) . ')'
. ' VALUES (' . implode(', ', array_values($insert_fields)) . ')';
sc_exec_sql($insert_sql);
Your SQL is evaluates as follows:
INSERT INTO test2 ( test2.PN ) VALUES ( test.ProjectNumber );
This is not valid because test.ProjectNumber is not understood in this context.
I think what you need to do is two separate insert statements as follows:
-- assumes that the number has not been inserted into test2 yet,
-- just insert the same value into both tables:
INSERT INTO test2 ( test2.PN )
VALUES ( somevalue );
INSERT INTO test ( test.ProjectNumber )
VALUES ( somevalue );
or if the first row has already been inserted (it's not clear form your question), you can select that value and insert it, but you need to be able to identify the row (I'm using 123)
-- If test2 already contains the row you need to insert from
-- then you need to select that row to insert the new row in test
INSERT INTO test ( test.ProjectNumber )
SELECT test2.PN
FROM test2
WHERE test2.Id = someid;
Hopefully this helps - I haven't translated this into php for you, partly as an exercise for you, but partly because my php is pretty rusty...
I'm trying to get the following code to run in a MySQL Trigger but I get error 1064 when I try to save it.
SET #ma = (SELECT modem_alias FROM `play`.`veh` WHERE meid = new.org_a LIMIT 1);
INSERT INTO `play`.`des` (`indx`, `des_a`, `des_b`) VALUES (NULL, new.org_a, SELECT #ma);
The trigger is set to run on 'org' table after an INSERT
Don't use SELECT in the INSERT, just the variable:
INSERT INTO `play`.`des` (`indx`, `des_a`, `des_b`) VALUES (NULL, new.org_a, #ma);
You can also combine the two queries so you don't need a variable:
INSERT INTO play.des (indx, des_a, des_b)
SELECT NULL, new.org_a, modem_alias
FROM play.veh
WHERE meid = new.org_a
LIMIT 1
Your values is wrong: You can't select there, just use the variable itself:
INSERT ... VALUES (..., #ma);
I've got this test table:
CREATE TABLE IF NOT EXISTS `test` (
`id` INT(10) AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4;
inserting using either of these three
INSERT INTO `test` (`id`) VALUES (NULL);
INSERT INTO `test` (`id`) VALUES (0);
INSERT INTO `test` () VALUES ();
and issuing
SELECT LAST_INSERT_ID();
but the query always results in 0.
PHP's mysql_insert_id and PDO::lastInsertId() yield no result either.
I've been toying with this whole day and can't get it to work. Ideas?
The problem seemed to be in MySQL's phpmyadmin config file PersistentConnections set to FALSE which resulted in a new CONNECTION_ID every time a query was issued - therefore rendering SELECT LAST_INSERT_ID() ineffective.
more info in the subsequent topic Every query creates a new CONNECTION_ID()
Also thanks dnagirl for help
Just my 50 cents for this issue, I simply noticed that you won't get a LAST_INSERT_ID greater than 0 if your table has no AUTO_INCREMENT set to an index.
I wasted about half hour on this. Turns out I keep getting a LAST_INSERT_ID() of 0, which for this table is actually ok.
you have to combine
INSERT INTO test (title) VALUES ('test');SELECT LAST_INSERT_ID();
Then you will get the last insert id
I had the same issue. mysql_insert_id() or LAST_INSERT_ID() returned 0 when requested inside a PHP function with an INSERT query.
I sorted the issue by requesting the value of mysql_insert_id() from a separate PHP function called right after the function that INSERT query.
I Had the same issues but I have resolved this by creating a transaction.
$db->begin();
$sql = "INSERT INTO 'test' VALUES('test')";
if ($db->query($sql))
{
$id = $db->last_insert_id('test');
$db->commit();
return $id;
}
else{
$db->rollback();
return -1;
}
I had tried
return $db->last_insert_id('test');
after the "commit" but that always returned "0"
hope that can help you
it work perfectly...try it...
$result = mysql_query("INSERT INTO `test` (`title`) VALUES ('test')");
if ($result) {
$id = mysql_insert_id(); // last inserted id
$result = mysql_query("SELECT * FROM tablename WHERE id = $id") or die(mysql_error());
// return user details
if (mysql_num_rows($result) > 0) {
return mysql_fetch_array($result);
} else {
return false;
}
} else {
return false;
}
I understand that there exists INSERT IGNORE and INSERT ... ON DUPLICATE KEY UPDATE. However, when there is a duplicate key, I'd like to do a INSERT to a temporary table to keep a record of the unique key that has been violated, so that I can output it to the user.
Is there any way I can do a ON DUPLICATE INSERT? If it helps, I'm trying to do a bulk insert.
With ON DUPLICATE KEY UPDATE, you cannot insert into another table - nor is there an alternative function available.
Two custom/alternative ways you can accomplish this:
Using a stored procedure as outlined in the accepted answer to this question: MySQL ON DUPLICATE KEY insert into an audit or log table
Creating a trigger that logged every INSERT for your table into another table and querying the table full of "logs" for any duplicates.
Something similar to this should work:
CREATE TABLE insert_logs (
id int not null
);
delimiter |
CREATE TRIGGER insert_logs_trigger BEFORE INSERT ON your_table
FOR EACH ROW BEGIN
INSERT INTO insert_logs SET id = NEW.id;
END;
|
To get a list of the duplicates in the table, you could us:
SELECT id FROM insert_logs HAVING COUNT(id) > 1 GROUP BY id;
Maybe this could be helpful:
$time = time();
$countme = 1;
while ($countme > 0) {
$stmt = $mysqli->prepare("INSERT INTO loads (dte,filecode,id,acct,nmbr) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE dte = dte");
$stmt->bind_param("sssss", $time, $filecode, $id, $acct, $number);
$stmt->execute();
$countme++;
if ($stmt->affected_rows === 1) {
$countme = 0; // all is good
} else {
$time = $time + 1;
}
if ($countme === 4) {
exit;
}
}
I am trying to insert into my table, but only if the information doesn't exist already.
This works:
$sql = //"if not exists (select `id` from friends where `id` = :id) " . //ERROR!
"insert into `friends` (`id`, `fb_id`, `name`, `id2`, `fb_id2`, `name2`) " .
"values (:id, :fb_id, :name, :id2, :fb_id2, :name2)";
try
{
$stmt = $this->db->prepare($sql);
if (!$stmt)
{
$errorcode = (string)
$stmt->errorCode();
trigger_error("mysql errorcode : " . $errorcode, E_USER_ERROR);
$data = array( "note" => "mysql error code: " . $errorcode );
return json_encode( $data );
}
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->bindParam(':fb_id', $fb_id, PDO::PARAM_INT);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':id2', $id2, PDO::PARAM_INT);
$stmt->bindParam(':fb_id2', $fb_id2, PDO::PARAM_INT);
$stmt->bindParam(':name2', $name2, PDO::PARAM_STR);
$result = $stmt->execute();
$stmt->closeCursor();
}
catch (Exception $e)
{
die ($e->getMessage() );
}
I tried if not exists ( select id ... etc as commeted out in the first line, but that didn't work. It returned error code 00000 with (string) $stmt->errorCode();, I googled it and it means: to many mysql connections, this can't be the problem as other queries work fine.
How do I insert into the table when not exist and how do I get mysql error information?
I've never seen your syntax before, putting an exists clause before the insert clause, but the following should work.
INSERT INTO `friends`
SELECT :id, :fp_id, :name, :id2, :fb_id2, :name2
FROM `friends`
WHERE NOT EXISTS ( select `id` from friends where `id` = :id )
LIMIT 1;
Just realized that you'll need at least one record in your friends table before this'll go.
insert IGNORE into `friends` (`id`, `fb_id`, `name`, `id2`, `fb_id2`, `name2`)
values (:id, :fb_id, :name, :id2, :fb_id2, :name2)
Or you want to prevent duplicate id's you can add a trigger
DELIMITER $$
CREATE TRIGGER bi_friends_each BEFORE INSERT ON friends FOR EACH ROW
BEGIN
DECLARE TestId integer;
SELECT id INTO TestId FROM friends WHERE id = new.id LIMIT 1;
IF (id IS NOT NULL) THEN BEGIN
/*force an error to prevent insert*/
SELECT * FROM force_an_error_table_unwilling_to_insert_dup_key;
END; END IF;
END $$
DELIMITER ;
Note that if id is a UNIQUE or PRIMARY KEY field inserting a duplicate key will create an error and rollback any pending transaction.
If you are using a non-transactional engine, MySQL will stop inserting rows at the error point leaving only part of your rows inserted.
Your question is vague as to why you want to do the IGNORE test. So I'll recap the options and the implications:
If you want a transaction to fail, set id as primary key and just insert as normal, the resulting conflict will generate an error and rollback the transaction.
If you want to just insert stuff with no worries about duplicate id's set field is to AUTOINCREMENT and replace the value for id with null. INSERT INTO table (id, fb_id,name,id,fb_id2,name2) VALUES (null,:fb_id,:name,:id2,:fb_id2,:name2)
If you just want the one insert not to succeed, do the INSERT IGNORE ... as stated above.
If you do not have transactions (MyISAM et al), you may have to do a test SELECT first and only INSERT a bunch of values after the test shows it is save to proceed.
You could use INSERT IGNORE assuming you have a unique index on id. See this post for discussion about the pros/cons: "INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"
This solution works even if you do not have any rows in your table.
INSERT INTO t (
x,
y,
z) (
SELECT
'00000',
'11111',
'22222'
FROM
DUAL
WHERE
NOT EXISTS (
SELECT
1
FROM
t
WHERE
c = '00000'
AND x = '11111'
AND y = '22222'
)
LIMIT 1
);