Inserting data which doesnt match the column count value - php

Got a question about inserting data (10 columns) in a table which has 30 columns. I've set all the default values at NULL and inserting like this:
INSERT INTO `app_res_per_form` VALUES ('monkey', 'bizon', 'Option one,Option two', '2', '1,2,3,4', 'charmender', 'dodo', 'bird')
Doesnt work because I get a Column Count doesnt match Row Count. I can only presume that is because I don't have 30 values but only 10. Or is it something different?
The table I want to insert it into has 1 Primary Key ID int(11) and the rest is TEXT columns which has a Default of NULL.

You need to specify the columns you want to insert to
INSERT INTO `app_res_per_form` (<col1>,<col2>,...) VALUES (<val1>,<val2>,...)
If the primary key is an auto increment then you do not need to include that column. If it is not auto increment then you do.

You still have to pass NULL in the query for the columns and specify ALL columns in the query

Related

how to insert a column value in mysql

A db table consists of 3 columns. But I want to insert a value to one column and I want the other 2 to remain empty. Like database has three input column- roll, date, time, I want only to insert roll. My query:
insert into tableName(roll) values ('$_POST['roll'])
But I am getting the error message ( Field attn_time doesn't have a default value41 ). What would be the correct query?enter image description here
database table
You need to alter your database table by giving a default value to your attn_time column.
Example:
ALTER TABLE table_name MODIFY COLUMN column_name VARCHAR(255) NOT NULL DEFAULT '';
You achieve this in two ways:
1) Go to your phpmyadmin/mysql and change your columns(attn, attn_time) "Null" property to "Yes". This will allow null value to store in column if non value given.
[You can find "Change" in "Structure" tab in phpmyadmin]
2) You can change your query to
insert into tbl1 (roll, attn, attn_time) values ('$roll', null, null)
OR
insert into tbl1 (roll, attn, attn_time) values ('$roll', '', '')
You can use :
$roll=$_POST['roll'];
add your validation :
insert into tableName(roll) values ('$roll');

mysql insert and update table with max 3

I have a php script that logs inputs from a form into a mysql database table. I'm looking for a way to insert this data untill 3 rows are created, after which it has to update the existing rows so that the first one updates to the new input, the second one to the former first input and the third one to the former second input.
Table:
CREATE TABLE IF NOT EXISTS inputlog (
id int(11) NOT NULL auto_increment,
userid int(11) NOT NULL default '0',
name text,
value text,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;`
For the first three rows i use:
insert into inputlog (userid,name,value) values('$userid','$name','$value')
After that is has to become:
update inputlog set value = '$value' where userid = '$userid' and name = '$name'
where it has to update all the successive rows.
How can i accomplish this?
Too long for comments, so...
Looks like you want to have only 3 rows in your table because you want the data to be sorted by the id. So id=1 will be the latest value, then id=2 and finally id=3.
In short, do not do that, the id field can be any value. Do not code for that. The danger is if you use the id in another table as a foreign key, you will loose referential integrity. What I propose is:
Add an timestamp column for each row.
Every time you insert a new value, set the timestamp column to NOW()
When selecting, sort on the timestamp and limit to 3 results
If you MUST have only 3 rows, you can then delete the row except for the 3 most recent timestamps.
But... if you must do that...
perform a SELECT with the first 2 lines
truncate the table (delete all rows)
insert the new line, then the 2 stored lines
You will then ahve your 3 rows in the order you want. But without seeing the entire reasoning for your application, my "spider sense" tells me you will hit a wall later on...
And check the comments for other things to worry about.

Field doesn't have a default value

I'm trying to follow along to https://netbeans.org/kb/docs/javaee/ecommerce/connect-db.html this for an assignment but I'm using my own entity relationship diagram in mySQL workbench.
As can be seen here https://www.flickr.com/photos/93791690#N02/23076476850/in/dateposted-public/
But when I try and follow what is said on the Netbeans site Delete 'select * from category' and enter the following SQL statement:
INSERT INTO `category` (`name`)
VALUES ('dairy'),('meats'),('bakery'),('fruit & veg');
But try with my own:
INSERT INTO `book` (`price`) VALUES ('20.0');
INSERT INTO `book` (`author_name`) VALUES ('author_name');
I keep getting errors saying
Error code 1364, SQL state HY000: Field 'author_name' doesn't have a default value
Line 1, column 1
Error code 1364, SQL state HY000: Field 'price' doesn't have a default value
Line 2, column 1
Execution finished after 0 s, 2 error(s) occurred.
Can someone please help me to start going in the right direction
Unless you want to insert two lines,
INSERT INTO `book` (`price`, `author_name`) VALUES ('20.0', 'author_name');
is likely what you want to do. The inserts trying to set just one column are failing because the other column has no default value. All columns which do not have a default value need to be set in an insert. If you intended to insert two rows here, then you'll need to make sure you specify values for both columns in each insert or ALTER your table so that the column has DEFAULT values. For example,
ALTER TABLE `book` MODIFY `author_name` varchar(200) DEFAULT '';
changing the size of the varchar to be whatever your author_name column is and replacing the empty string '' with whatever you want the default to be.

How to get MySQL value from certain row?

I have a table with 5 rows. Every time a user enters data into a form, it is entered into the table. My first column is called id and holds the number of the post. What I want to do is get the value of id from the previous row, add one to it and set it as the value in the current post's id field. How do I do this?
Just set that field as primary key and auto-increment, it will automatically do this for you. You won't have to fetch the previous row and add that field value to next one.
The SQL query you need is:
SELECT max(id)
FROM tableName;
Set attribute auto increment for "ID" field in the table that contains 5 columns.
You can use sql query like
"INSERT INTO my_table (id auto_increment,primary key(id))";
then you can get...
and eachtime you need not worry to insert id ,it will automatically increments
I would not recomend doing this as it could lead to a race condition.
Change the table structure and set the id field to be the primary key and set it to auto increment. This way anytime a new row is added, it will auto-magically be assigned the next ID.
see this answer on details of how to set auto increment.
here is the query to alter your table and it will set your field or column as primary key and also auto increment it.
ALTER TABLE tbl ADD id INT PRIMARY KEY AUTO_INCREMENT;

copy several mysql rows based on foreign key

I want to copy several rows in a mysql table based on a foreign key, and asign the new rows a new foreign key ID. Assuming my table layout looks like this:
test
-----
table1_id int(11)
value varchar(20)
How do I accomplish this?
Found out that the query would need to look like this:
INSERT INTO test (table1_id, value) (SELECT '2', value FROM test WHERE table1_id=1)
which would copy all rows with the foreign key ID '1' and assign the new rows with ID 2 instead. If the table contains more rows you could add them or change order in the SELECT part, like this
...(SELECT row1, '{$new_id}', value, another_row FROM...

Categories