In my form, I have file type input field that is dynamically generated by add more click button.
Everything is ok, when I insert the data but i'm facing problem when I update second table which
I used for only store file type data.
I have analysed some case:
Case 1: if value exist is equal to new value then update
Case 2: if value not exist then insert data
Case 3: if value exist and greater than previous value then update the previous value and insert new data
case 4: if value exist and less than previous value then update previous and other value will be remain same(no change).
I have tried so far
$array_filename = trim($array_filename, ",");
$filename_array = explode(',', $array_filename);
$query = mysql_query("SELECT * FROMtbl_name_2WHEREjob_app_id='$editId' ");
$res = mysql_fetch_array($query);
for($i=0;$i<coun($filename_array);$i++) {
$insertQuery = "REPLACE INTOtbl_name_2(id,job_app_id,file_name) VALUES ('$res[$i]','$editId','$filename_array[$i]')";
$isInsert = mysql_query($insertQuery) or die(mysql_error());
}
mysql update if exist else insert
Should better be described as INSERT ELSE UPDATE - and that's what you can to with the ON DUPLICATE KEY UPDATE keywords and the proper unique key constraints (assuming uniqueKey column to be unique):
INSERT INTO
myTable (`id`, `uniqueKey`, `value2`) VALUES (null,"test1",17)
ON DUPLICATE KEY UPDATE
value2 = 17;
This covers your case 1 and 2: Data is inserted or updated.
For your case 3 and 4 it is unclear, what you are asking? It seems like these statements are targeting multiple tables, or what do you mean by update the previous value and insert new data?
Related
I have a localhost and I want to do something I hope I can explain it.
I have a table named 'students' in a database named 'theway', there is a column named 's_class_id' its kind is auto increment , and in php I insert some values in this table. But I want to insert a variable+the auto increment in the column named 's_class_id'.
For example : The variable's value is 'a', and the auto increment value is '5'. I want the value which be inserted in the 's_class_id' like this 'a5'.
Any help?
If it is auto-increment, the field should be an integer. Do not think that it is possible to change it to eg: 'a5'. You could run an update query searching for the s_class_id and update another string field to set it to be 'a5'.
You can do that task by folowing method,
First you need to insert record into theway table. After record insertion mysqli_insert_id($con) will return last inserted id. Then you need to update that row by using last inserted id.
// Insert Record
mysqli_query($con,"INSERT INTO theway (s_class_id) VALUES ('a')");
// Getting last inserted id
$lastInsertID = mysqli_insert_id($con);
$updatedValue = 'a'.$lastInsertID;
// Update same row with last inserted ida
$SameRowUpdate = "UPDATE theway SET s_class_id='".$updatedValue."' WHERE id=".$lastInsertID;
if (mysqli_query($conn, $SameRowUpdate )) {
echo "Record addedd successfully";
}
I want to insert one value into a row. This row has multiple values. When I try to insert show error row-1 does not have default value.
$NoName = "Record_name";
$no = "20"; // I want to INSERT this no in row
$sql4 = "INSERT INTO record (date, month, year, $NoName) VALUES ('12-12-2016','12','2017', '$no')";
$result4 = mysqli_query($mysqli,$sql4) or die(mysqli_error($mysqli));
There is some more other rows like no 1, no 2,no 3 i just want to insert no 1 value in new row other values will be empty.
You can add a default value to that column:
ALTER TABLE record ADD DEFAULT '0000-00-00' FOR date
This is from :
How to set a default value for an existing column
Note that you should use a valid default value.
I'm trying to create a statement that inserts and updates rows in my database which changes a value in my database when values has changed while updating the row.
I'm not sure if my question is clear enough but here is my code (check the comments, this should clear things up):
$stmProducts = $db->prepare("
INSERT INTO
products
SET
identifier = :identifier,
title = :title,
price = :price,
content = :content
ON DUPLICATE KEY UPDATE
title = :title,
price = :price,
content = :content
// If any values (title, price or content) has been changed while updating:
// updated = true
");
I've tried something with CASE WHEN but that didn't work, I don't have the exact code I used but it was something like this:
updated = CASE WHEN title != :title THEN true END
I could be looking into the completely wrong direction but what's the best way to get to what I'm trying to achieve?
You can know it examining PDOStatement::rowCount(). With an insert it will return 1, while with an update will return 2.
Check the documentation
With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if
the row is inserted as a new row, and 2 if an existing row is updated.
I have a mysql question here. What I'm trying to do is create a button ('Add to Wishlist') that, when pressed, executes a MYSQL update query that enters a number, say 6, into multiple fields, however I'm trying to get is so that it only enters in one field that is not null. So in essence, the update query will look to see if field one (saved_courses) is empty, and if it is then insert value, but if it isn't then insert into the second field (saved_courses2).
I've looked into this and as a result of my research I have this:
mysqli_query($con, "UPDATE user_accounts SET saved_courses3 = case when saved_courses3 is null then saved_courses3 = $urlid else saved_courses4 = $urlid end WHERE id = 1; ") or die(mysql_error());
Try something like this:
UPDATE user_accounts SET
saved_courses4 = case when saved_courses3 is null then saved_courses4 else $urlid end,
saved_courses3 = case when saved_courses3 is null then $urlid else saved_courses3 end
WHERE id = 1
You can see it in action at:
http://sqlfiddle.com/#!2/c5553/1
I'm trying to update a row or insert a new one if it exists. If there's an UPDATE I just like to update the "updated" column with the current timestamp otherwise "added" AND "updated" get the same value (timestamp)
//1385982893 is from PHP with time() cause it's needed elsewhere too
INSERT INTO table (id, code, added, updated) VALUES (236, 'abcdefghi', 1385982893, 1385982893)
ON DUPLICATE KEY UPDATE
id = values(id), code = values(code), added = values(added),updated = 1385982893
ID is the primary key. code is UNIQUE
The problem is that "added" always gets updated with the current timestamp (like updated)
You just need to to remove the added = values(added) and it won't get updated:
INSERT INTO table (id, code, added, updated) VALUES (236, 'abcdefghi', 1385982893, 1385982893)
ON DUPLICATE KEY UPDATE
id = values(id), code = values(code),updated = 1385982893
Then do not set added in Update
INSERT INTO table (id, code, added, updated) VALUES (236, 'abcdefghi', 1385982893, 1385982893)
ON DUPLICATE KEY UPDATE
id = values(id), code = values(code), updated = 1385982893