简体   繁体   English

如何在mysql中插入列值

[英]how to insert a column value in mysql

A db table consists of 3 columns.一个 db 表由 3 列组成。 But I want to insert a value to one column and I want the other 2 to remain empty.但是我想向一列插入一个值,而我希望其他 2 列保持为空。 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 ).但我收到错误消息(字段attn_time没有默认值 41 )。 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.您需要通过为attn_time列提供默认值来更改数据库表。

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". 1) 转到您的 phpmyadmin/mysql 并将您的 columns(attn, attn_time) “Null”属性更改为“Yes”。 This will allow null value to store in column if non value given.如果没有给出值,这将允许空值存储在列中。 [You can find "Change" in "Structure" tab in phpmyadmin] [您可以在phpmyadmin的“结构”选项卡中找到“更改”]

2) You can change your query to 2)您可以将查询更改为

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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM