Im currently using CodeIgniters active record class to UPDATE a MySQL table. One of the columns is an INT who's default value I've set to NULL and set the NULL field to TRUE. When I INSERT a record and leave that particular field blank in the form, no problem, it's set to NULL. However, when I do an UPDATE with the field blank in the form, MySQL sets the value to 0. Is there a way to have MySQL intepret an UPDATE the same way an INSERT is done i.e. if the form value is blank it sets it to NULL and not 0?
Cheers
Most likely empty string '' is sent to the database thus it is cast to INT field as 0 - check exact SQL query (eg. by enabling profiler: $this->output->enable_profiler(TRUE) in your controller). If that's the case add a snippet to your model that will replace '' with NULL while updating.
Have you run a trace on the database to see exactly what SQL CodeIgniters is sending the database? It sounds like CodeIgniters may be sending the zero to MySQL...
Related
If the user manually reverts a change to a field in a linked table in Access resulting in the record being unmodified overall, the 'Write Conflict' error message will be displayed when attempting to save the record. An example would be that the user makes a single change to a record by changing a field from 10 to 20, and then without saving the record re-enters the original value of 10.
Office Pro Plus 2016
MySQL 8.028
To reproduce this behaviour:
Create a test database in MySQL
CREATE SCHEMA test;
USE test;
CREATE TABLE testtable (
ID INT NOT NULL,
ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
col1 INT NULL DEFAULT NULL,
col2 INT NULL DEFAULT NULL,
PRIMARY KEY (ID))
ENGINE = InnoDB;
Create an ODBC connection to the test database using a a user DSN and ANSI MySQL driver
Create a blank access database and link to the testtable
Open testtable and enter and save a couple of dummy rows. Change the value in one of the col fields. Without navigating away from the row or using undo, type the original value back into the field. Attempt to save the row.
I have deliberately included timestamp to demonstrate that this is not a fix.
I would appreciate advice from anyone who knows what causes this behaviour and how to prevent it.
I found a workaround for this which is not ideal but it does work. I've added a boolean field to the SQL table called changeflag. In the OnDirty event of the Access form I call a simple procedure to change the value of this field. This way even if the user manually undoes a change that they made, there is still a change to the data somewhere else and the write conflict no longer occurs. The code is below.
Private Sub Form_Dirty(Cancel As Integer)
changeflag = Not changeflag
End Sub
Turns out the solution is buried in the MYSQL ODBC documentation, the FOUND_ROWS connector/ODBC option needs to be set. In the GUI, it's under Cursor/Results tab of the Data source configuration: 'Return matched rows instead of affected rows'. After changing this setting, the conflicts no longer occur.
I'm creating a user registration form and when I get to confirming the user's email by emailing them, I need some sort of unique string to confirm against.
Instead of generating one in PHP and inserting it into the database, I wanted to try and add a column to my table that would hold a unique value that I could use whenever I needed to confirm something.
What I want to do is set the value to an MD5 of the current timestamp. I tried just doing SELECT MD5(CURRENT_TIMESTAMP) in phpMyAdmin just to see if it would let me and it did so I thought I'd add that to an update condition but it doesn't seem to be letting me.
ALTER TABLE users ADD confirmation VARCHAR(40) DEFAULT NULL ON UPDATE MD5(CURRENT_TIMESTAMP);
The above is what I've tried. I get an error and I don't know how else to do it.
Is there anyway I can do this or something similar? Side question, does the ON UPDATE trigger on a row that just got inserted?
The syntax you are trying to use doesn't exist. It looks like you are thinking of ON UPDATE CURRENT_TIMESTAMP but that is a rather specific command, as per https://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is
specific to TIMESTAMP.
So the ON UPDATE clause only works with CURRENT_TIMESTAMP and only on fields of type TIMESTAMP.
If you want to use the MD5 of the current timestamp either set a trigger, or just manually set the value (e.g. UPDATE users SET confirmation=MD5(CURRENT_TIMESTAMP()) WHERE user_id=123).
Bare in mind that the MD5 of the current timestamp is something that could be quite easily guessed / brute forced, so don't rely on it for security.
Use a universal unique identifier for this purpose. It's a 128-bit unique number; it's designed for this kind of thing.
It has a string representation that fits in 36 bytes.
aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
You can generate these things in most programming languages. In MySQL you use the UUID() function to get one. Every time you call UUID(), you're guaranteed to get a new value.
Add a CHAR(36) column to your database, or just use your VARCHAR(40) column.
You can't use data definition language (ALTER TABLE) to declare ON UPDATE except for a native timestamp. You'll need application code to set your UUID values, just like you do for MD5(CURRENT_TIMESTAMP).
I just wanted to know if somebody could explain this.
I was just testing my code and didn't check for empty input fields (I know I have to, but just testing).. In my database table, all the fields are NOT NULL, and I was expecting a exception because I wasn't inserting anything.. But it turns out that MySQL inserts all with blank values, also, from MySQL workbench is the same thing..
Is there a way to prevent this? (From a MySQL perspective)
This behavior, although atypical, is quite well documented:
Inserting NULL into a column that has been declared NOT NULL. For
multiple-row INSERT statements or INSERT INTO ... SELECT statements,
the column is set to the implicit default value for the column data
type. This is 0 for numeric types, the empty string ('') for string
types, and the “zero” value for date and time types. INSERT INTO ...
SELECT statements are handled the same way as multiple-row inserts
because the server does not examine the result set from the SELECT to
see whether it returns a single row. (For a single-row INSERT, no
warning occurs when NULL is inserted into a NOT NULL column. Instead,
the statement fails with an error.)
So, if you want to get an error, use VALUES() with a single row. Alternatively, define a trigger that does the check.
Why does MySQL work this way? I don't know, to differentiate itself from other databases and prevent ANSI-compatibility? More seriously, I assume that this a question of efficiency, and related to the fact that MySQL does not implement check constraints. The NOT NULL declaration is just an example of a check constraint, and these are not supported.
My web application receives an update form for a db record, submitted by the user.
I would like to create an sql which will update all the values to exactly what the user submitted, except those which the user left blank. For those, I want the value to be set to its DB default.
Is there a way to do it?
I'm looking for something like
update my_table set col1=17, col2=DEFAULT, col3='some text'
Please notice that I'm updating an existing row, thus I cannot just live some columns out from the update sql, as they might have had a value before which needs to be erased now.
There is a DEFAULT(col_name) function:
Returns the default value for a table column. An error results if the
column has no default value.
UPDATE my_table SET col1=17, col2=DEFAULT(col2), col3='some text'
Well apparently it's just that simple, exactly as I wrote:
update my_table set col1=17, col2=DEFAULT, col3='some text'
http://dev.mysql.com/doc/refman/5.0/en/update.html
i am using mysql as back end in my android app but i want to do some if any user try to enter null record (empty fields) than it can't write in mysql table is it possible to prevent to entering null record in table.
You can mark the columns as NOT NULL. This will prevent any NULL value to be entered in those columns.The query will fail to execute if a NULL value is entered. In the front end you can display an error msg if the if the query fails, to let the user enter the correct value.
Do validation when you are entering value into mysql, Or you can store default value for that column if front end side field is not compulsory.
Always do a validation do not push data into database blindly.