The thing is that i have some dynamic columns and i use always the same php ajax page. So i get from the other page some coma separated strings. One with the columns, other with the table and other with the data. In the insert there is no problem because i do it this way:
$query = 'INSERT INTO '.$_GET["table"].' ('.$_GET["columns"].') VALUES('.$_GET["data"].')';
mysql_query($query, $link);
That transforms to this:
INSERT sys_users_cfg (usr,pwd,permission_id,image) VALUES ('pepwe2','1234','1','')
I need to do the UPDATE in the same way. with VALUES statement. Like:
UPDATE sys_users_cfg (usr,pwd,permission_id,image) VALUES ('pepwe2','1234','1','') WHERE usr_id = 33
That dosnt work. Is this posible?
Use the REPLACE INTO syntax instead if you're updating using the primary key, e.g.
REPLACE INTO sys_users_cfg (usr_id,usr,pwd,permission_id,image) VALUES (33,'pepwe2','1234','1','').
MySQL update is in a different format, see http://www.tizag.com/mysqlTutorial/mysqlupdate.php
UPDATE table SET column = 'value', column2 = 'value2' WHERE id = ID
This is how you update a row :
UPDATE sys_users_cfg SET usr = 'pepwe2', pwd = '1234', permission_id = 1, image = '' WHERE usr_id = 33
You can go read http://dev.mysql.com/doc/refman/5.0/en/update.html for more information.
$values = explode(',', $_GET['data']);
$query = "UPDATE sys_users_cfg SET usr = '$values[0]', pwd = '$values[1]', permission_id = '$values[2]', image = '' WHERE usr_id = '33'";
Related
I have some table in my SQLite3 database , for example
table1
table2
table3
All tables have a field called id (integer NOT NULL PRIMARY KEY UNIQUE) (not auto increment)
My goal is to duplicate a record of one of these tables, changing only the id field (with a new id). I would like to do this without having to know the name of all the columns in each table.
I tried this solution, it works, but in this way i have to know the names of all columns
INSERT INTO table1 (id,column1,column2,column3)
SELECT NEWID,column1,column2,column3 FROM table1
WHERE id = OLDID
I tried also the method to create a temporary table, but without success
I have to run a query like this PRAGMA table_info(table1) , save the columns's name in an array , and then run a query created with a cycle ?
thanks
My solution
$TABLENAME = "mytablename";
$ID = 1423659222480;
$NEWID = 1423659222481;
$db = new PDO('sqlite:db.sqlite3');
$result_columns = $db->query("PRAGMA table_info(".$TABLENAME.")");
$appColumns = array();
$appColumns2 = array();
foreach ($result_columns as $row) {
array_push($appColumns, $row["name"]);
if($row["name"] != "id") array_push($appColumns2, $row["name"]);
}
$appColumns = implode(",",$appColumns);
$appColumns2 = implode(",",$appColumns2);
$appColumnsWithoutID = $NEWID.",".$appColumns2;
$queryDuplicate = "INSERT INTO ".$TABLENAME." (".$appColumns.") SELECT ".$appColumnsWithoutID." FROM ".$TABLENAME." WHERE id = ".$ID;
Ok is there a possibility to update a column instead of a row?
f.e something like that:
$laninpstmt = $db->prepare ("UPDATE table SET column_name WHERE id = :allids");
$laninpstmt->bindParam(':allids', $_POST['input0']);
$laninpstmt->bindParam(':allids', $_POST['input1']);
$laninpstmt->bindParam(':allids', $_POST['input2']);
$laninpstmt->bindParam(':allids', $_POST['input3']);
$laninpstmt->bindParam(':allids', $_POST['input3']);
If i explain the code it's like:
Update all the rows(allids) from one column in a table
Running your query without a where clause will update all rows, and if you update a single field it will be the same as updating a column
UPDATE `test` SET `field_5` = 7
Will update table test and set all values in the column field_5 to 7
You could use IN:
Apparently, you need to do your own query, see https://stackoverflow.com/a/920523/2575355.
$inputs = array($_POST['input0'], $_POST['input1'], $_POST['input2']);
$allids = implode(', ', $inputs)
$laninpstmt = $db->prepare ("UPDATE table SET column_name WHERE id IN ($allids)");
You forgot to specify the value for you column_name like that
UPDATE table SET column_name = 'Some_value here' WHERE id = :allids
i guess you want do this
$laninpstmt = $db->prepare ("UPDATE table SET column_name = Concat(:allids1 , :allids2, :allids3, :allids4) WHERE id = :allids");
$laninpstmt->bindParam(':allids', $_POST['input0']);
$laninpstmt->bindParam(':allids1', $_POST['input1']);
$laninpstmt->bindParam(':allids2', $_POST['input2']);
$laninpstmt->bindParam(':allids3', $_POST['input3']);
$laninpstmt->bindParam(':allids4', $_POST['input4']);
I am trying to update an entry in my database.
My query is as follows:
UPDATE asc_student_appointment
SET
RANDOM_ID = '5i0oqotp6stiri9awo9ptp0o5aeoqpta4awi3o-i',
STUDENT_ID = '123456789',
FIRST_NAME = 'Testy',
LAST_NAME = 'McTesterson',
RIT_EMAIL = 'test#test.edu',
PHONE_NUMBER = '555-555-5678',
DATE_OF_APPOINTMENT = '2013-10-31',
TIME_OF_APPOINTMENT = '4:00 PM',
STAFF_NAME = 'JOHN DOE',
ADMIN_EMAIL = 'admin#test.edu'
WHERE
RIT_EMAIL = 'test#test.edu'
AND
STUDENT_ID = '123456789'
Now.. I want to update the entry for Testy in the asc_student_appointment table with the data provided in the query. The query is executing correctly, however, 0 rows are being affected.
I know that Testy exists in the database, however I don't understand why his information is not being updated.. The query executes fine but changes no data.
Any help?
Try to do a SELECT first to see if there is any row to update:
SELECT * from asc_student_appointment WHERE RIT_EMAIL = 'test#test.edu' AND STUDENT_ID = '123456789'
If there is no row, you will have to do an INSERT instead of UPDATE
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySQL trigger to update a field to the value of id
Please I have a table with three fields :
Id (auto increment)
GroupById
Text
And I want when inserting a new row : If I left the field groupById blank, it must get by default the same value of the field Id.
Please have you any Idea ? Thanks in advance.
Edit : My code :
mysql_query("INSERT INTO group SET GroupById = (must get the current Id), Text = 'bla bla bla' ");
How about a trigger:
DELIMITER $$
CREATE TRIGGER trg_yourtable
BEFORE INSERT ON yourtable
FOR EACH ROW
BEGIN
IF NEW.GroupById IS NULL THEN
SET NEW.GroupById = (
SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'yourtable'
);
END IF;
END $$
I'm not sure how safe this is... or what happens when you insert multiple rows from one query, or when two connections attempt to insert at the same time... but it works.
This simple SQL should do what you want:
INSERT INTO myTable (GroupById, Text) VALUES (NULL, 'your text');
SET #lastID = LAST_INSERT_ID();
UPDATE myTable SET GroupById = #lastID WHERE Id = #lastID;
Use a stored procedure
Get the MAX id
Add 1 to it and add insert it into both rows
You get the desired result in a single transaction.
Hope this helps.
Use this function | Tested
Example of use:
function get_current_insert_id($table)
{
$q = "SELECT LAST_INSERT_ID() FROM $table";
return mysql_num_rows(mysql_query($q)) + 1;
}
$txt = "text";
$groupID = '';
if ( empty($groupID) ) { $groupID = get_current_insert_id(test); }
$query = mysql_query("INSERT INTO test VALUES ('', '$groupID', '$txt') ");
if ( $query ) { echo 'Saved using ID:' . $groupID; } else { echo 'Oh noes!' . $query; }
Looks like you might need to run 2 queries:
insert into 'table' ('GroupById', 'Text') VALUES ('{group id}', '{sometext}')
update 'table' set GroupById = id where GroupById = null - it mightbe 0 instead of null depending on what you insert in db.
You can always optimize it through indexes, limits, order.
$query = mysql_query("UPDATE a SET fruit = '**apple**' WHERE id = '**1**' ");
$query2 = mysql_query("UPDATE a SET fruit = '**orange**' WHERE id = '**2**' ");
$query3 = mysql_query("UPDATE a SET fruit = '**peach**' WHERE id = '**3**' ");
is there any way to simplify it to one query?
I found a following solution:
INSERT into `table` (id,fruit)
VALUES (1,'apple'), (2,'orange'), (3,'peach')
ON DUPLICATE KEY UPDATE fruit = VALUES(fruit);
Id must be unique or primary key.
But don't know about performance.
Yes you can do it using this query:
UPDATE a
SET fruit = (CASE id WHEN 1 THEN 'apple'
WHEN 2 THEN 'orange'
WHEN 3 THEN 'peach'
END)
WHERE id IN(1,2 ,3);
Using IF() function in MySQL this can be achieved as
UPDATE a
SET fruit = IF (id = 1, 'apple', IF (id = 2, 'orange', IF (id = 3, 'peach', fruit)));
Based on the warning message
'VALUES function' is deprecated and will be removed in a future release. Please use an alias (INSERT INTO ... VALUES (...) AS alias) and replace VALUES(col) in the ON DUPLICATE KEY UPDATE clause with alias.col instead
One may consider a slight modification of Yaroslav's solution like so:
INSERT into `table` (id,fruit)
VALUES (1,'apple'), (2,'orange'), (3,'peach') as tb
ON DUPLICATE KEY UPDATE fruit = tb.fruit;
It does just the same thing but mutes the warning message.