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.
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";
}
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?
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
I have the following table
id year name activation
1 2013 TEST 1
id A_I
year, name UNIQUE
name, activation UNIQUE
I use this query to INSERT/UPDATE data:
INSERT INTO LISTE_DATI
(year, name, activation)
VALUES
('$varray[1]', '$varray[2]', '$varray[3]')
ON DUPLICATE KEY UPDATE
year= '$yr',
name= '$na',
activation= '$act'
If I send this data to the table:
$yr = 2014
$na = TEST
$act = 0
the query INSERT data in the table. This is ok for me!
If I send this data to the table:
$yr = 2015
$na = TEST
$act = 1
the query UPDATES the first row (2013/TEST/1) in the table.
In this case I'd like to have an INSERT too.
How can I adjust it?
You are telling your INSERT query, that when it finds a duplicate (UNIQUE) key, to instead update that row.
You are inserting (2012, 'TEST', 1). This is a duplicate key; the name, activation key, your 2nd UNIQUE key! You already have a row with 'TEST', 1; the row with id=1.
The INSERT query updates that row, since it's a duplicate key.
You need to modify the keys on your table so that it reflects the data you want in it. What do you want the INSERT query to consider a duplicate? Create your UNIQUE keys based on that.
I am inserting multiple rows using one query and, obviously, the ID column auto increments each row. I want to create another ID column and have the ID remain the same for all rows inserted during the query. So if I insert 10 rows during one query, I want all 10 rows to have the id "1". How can this be done? Thanks for any help
If I understood your question correctly, you want to supply an ID for the specific group of INSERT statements.
Assumming you have this schema
CREATE TABLE TableName
(
RecordID INT AUTO_INCREMENT PRIMARY KEY,
OtherColumn VARCHAR(25) NOT NULL,
GroupID INT NOT NULL
)
You can have two statements for this:
1.) Getting the last GroupID and increment it by 1.
SELECT COALESCE(MAX(GroupID), 0) + 1 AS newGroupID FROM TableName
2.) once you have executed it, store the value in a variable. Use this variable for all the insert statement,
$groupID = row['newGroupID'];
$insert1 = "INSERT INTO TableName(OtherColumn, GroupID) VALUES ('a', $groupID)";
$insert2 = "INSERT INTO TableName(OtherColumn, GroupID) VALUES ('b', $groupID)";
$insert3 = "INSERT INTO TableName(OtherColumn, GroupID) VALUES ('c', $groupID)";
UPDATE 1
SQLFiddle Demo