I just this error when Im updating database and I dont change any of the values in the form and submit. I would like to know why and suggestion to correct this error.
sample
SELECT * FROM `table` WHERE `id` = 1
id = 1,
name = John,
city = New York;
UPDATE `table` SET name = 'John', city = 'New York' WHERE id = 1
when updating the database with the same values what you select from database and use affected rows i get 0
Enclose the string values in quotes (')
UPDATE `table` SET name = 'John', city = 'New York' WHERE id = 1
As the manual states:
For UPDATE statements, the affected-rows value by default is the number of rows actually changed. If you specify the CLIENT_FOUND_ROWS flag to mysql_real_connect() when connecting to mysqld, the affected-rows value is the number of rows “found”; that is, matched by the WHERE clause.
Therefore, because your UPDATE does not actually change any rows, the default affected-rows value is 0.
Related
I'm using MySQL 5.7 and for some reason my INSERT statement isn't working as before even though the syntax looks correct. It's error-ing out on the where statement...
SQL:
insert into users(age) values('16') where username='r';
If the row for username r already exists perhaps you are looking to update the age value instead?
Use: update users set age = 16 where username = 'r' instead.
Also, I'm just guessing here, but maybe age holds a numeric value, and if so you can remove the quotes around 16.
That syntax isn't correct. You can't use where like that. Perhaps you want something like:
insert into users (username, age)
values ('r', '16')
http://dev.mysql.com/doc/refman/5.7/en/insert.html
Alternatively if that user already exists, you might be looking for an update statement instead:
update users set age = '16' where username = 'r'
INSERT statements must not contain a WHERE clause. Remove it.
If, in fact what you are trying to do is update an existing row, use an UPDATE statement, not an INSERT statement.
update users set age = '16' where username='r';
If you want to insert new records in your table, you have to write query for inserting data.
SQL INSERT INTO Statement syntax is this:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
In your case, if you don't have the record in your database, your query will look like this:
INSERT INTO users (username, age)
VALUES ('r', '16')
But if you want to update existing records in your table, you have to write query for updating data using the SQL UPDATE Statement.
The syntax for this is:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
To update the record/s, you have to specify clause in WHERE which record should be modified.
To modify age of user/s with username that is equals 'r', this is the query:
UPDATE users SET age = 16 WHERE username = 'r'
but if you want to modify for all users which usernames starts with 'r':
UPDATE users SET age = 16 WHERE username = 'r%'
I hope this explanation will help you to understand better SQL statements for INSERT new and UPDATE existing records.
I'm trying to run a single SQL command which will update table 'name', where the column 'id' matches 'eg1', 'eg2' and 'eg3'. The column to be updated is 'status' and should be changed to 'new_status' for the previously specified ids only.
Unfortunately I'm new with SQL, therefore I only got as far as this which doesn't seem to be working:
SELECT * FROM `tblhosting` WHERE 'id' IN (eg1,eg2,eg3) UPDATE 'status' SET new_status
Update tblhosting set status = 'new_status' where id in ('eg1','eg2','eg3')
This assuems you want to update tblhosting column status set to 'new_status' where the ID is either eg1, eg2 or eg3.
String literals are enclosed in single quotes.
Identifiers can be optionally enclosed in backtick characters.
The syntax for an UPDATE statement is like this:
UPDATE `tblhosting`
SET `status` = 'new_status'
WHERE `id` IN ('eg1','eg2','eg3')
The specification is a little ambiguous. The example above searches the table named tblhosting for rows to be updated, and assigns a value to the status column. This assumes that the value to be assigned is a string literal "new_status", and that "eg1", "eg2" and "eg3" are string values found in the column named id.)
update tblhosting set new_status = 'status' WHERE 'id' IN (eg1,eg2,eg3)
Assume I have the following in my users table:
id name email
'1', 'foo', 'foo#bar.com'
'2', 'foo2', 'foo2#bar.com'
'3', 'foo3', 'foo3#bar.com'
'4', 'foo4', 'foo4#bar.com'
Question 1)
If I want to delete a column data for one of the fields. Is it safe if I set it to NULL (Please see the following update query in my example); in other words is it safe for all data types in mysql (int, varchar, bit,...) to assign NULL to make it empty?
UPDATE users
SET email = NULL
WHERE id = 3;
The above update query empties email field for user with id 3, but please confim this is the valid solutoin for all datatypes...
Question2)
After making the email field empty for user with id of 3 the following query is returning nothing:
select * from users where email is NULL
Am I doing something wronmg here?
Please let me know if you need more clarification if the question is vague...
Thanks
If NULL is allowed for the column in the table definition, then you can set NULL to any data type. Your code should work. What happens when you run "select * from users" in a command line or use a database manager?
UPDATE users
SET email = ''
WHERE id = 3;
So I have this big table with over 30 columns, and what I wanted to do was insert its values but ofc skipping the first column which is the 'id' that's auto incremented, so I had two choices that I can think of, one is:
INSERT INTO 'table name' col2,col3,col4... VALUES val2,val3,val4
But that would've taken a long time to copy the names of the columns, and a possibility of missing a letter thus having an error. So another option would be fetching the last id in the table and editing the next id myself:
$last_row_query = mysql_query("SELECT id FROM `order` ORDER BY id DESC LIMIT 1");
if(!$last_row_query) {
die('invalid query: '.mysql_error());
}
$last_row = mysql_fetch_assoc($last_row_query);
$last_id = $last_row['id'];
$current_id = $last_id+1;
and then the query would be
INSERT INTO 'table name' VALUES $current_id,val2,val3,val4
Is there's any difference in the efficiency of the those two ways? And is there a way that's recommended more than the other?
If you fetch the current value and try to insert the next higher value, you create a race condition. Two concurrent app requests would try to insert the same value.
A better solution is to use either NULL or DEFAULT in place of a value. Zero also may work, depending on the SQL mode.
INSERT INTO `table name` VALUES (DEFAULT, ?, ?, ?);
And remember to put the table name in back-ticks, not single-quotes. Single-quotes are for string literals or date literals.
I have a table (foo) where I already have a PK on id:
id name rank
-----------------------
1 AAAA 2
2 BBBB 1
I want to insert a new row where I know the values of column id and name and want rank to take a value greater than any other value in the same column in preceding rows (similar to what auto_increment does for us).
i.e. if I were to add a row with value = CCCC, the rank column should have a value 3. I need to do this in a compound statement if possible. I tried the following which does not work.
insert into foo (`name`, `rank`)
values ('CCCC', (select max(`rank`) from `foo`))
Which gives me the following error:
You can't specify target table 'foo' for update in FROM clause
Note: I would ideally like to have the rank column as an auto_increment field, but apparently that's not allowed either, since I already have a PK.
PS: I need to be able to execute this statement from PHP without using stored procedures.
Try this first, it being derived instantly from your post:
INSERT INTO foo (`name`, `rank`)
SELECT 'CCCC', (MAX(`rank`) + 1) AS rank
FROM `foo`
Then using PDO, I think this'll work:
...
$sql = "INSERT INTO foo (`name`, `rank`) SELECT ?, (MAX(`rank`) + 1) AS rank FROM `foo`"
$name = "CCCC";
$st = $pd->prepare($sql);
$st->bindValue(1, $name);
try {
$retval = $st->execute();
} catch (PDOException $pdoex) {
...
Not sure if I got in syntactically correct but that should be about the gist of it ... I think
Err.. lemme know if the SQL works, at least :D