简体   繁体   English

当带有默认值和 null str 在用户输入中使用 php 和 Z81C3ZB080DAD53EBF587E1

[英]how can i use a uniform sql for insert when come with default value and null str in user inputs with php and mysql

Here I have a mysql table like this:在这里,我有一个像这样的 mysql 表:

create table abc (
    name varchar(20) not null,
    value int not null default 20
)

and in the php page, I wanna use a uniform sql for all insert like this:在 php 页面中,我想对所有插入使用统一的 sql,如下所示:

insert into abc values('first name', '30')

but sometimes, there maybe someone dont input a value and the insert sql will be like但有时,可能有人不输入值,插入 sql 会像

insert into abc values('a name', '')

and why can't it work?为什么它不能工作? The mysql says Incorrect integer value: '' for column... and I want it give the default value. mysql 说Incorrect integer value: '' for column...我希望它给出默认值。 How can I do that?我怎样才能做到这一点?

You can't insert a not-integer-value into your field if you have declared it as an integer with NOT NULL .如果您已将非整数值声明为 integer 而NOT NULL ,则您不能在字段中插入非整数值。

1) Either control the value via your code if you want to use the same INSERT statement in both cases... 1)如果您想在两种情况下使用相同的INSERT语句,请通过您的代码控制值...

$var = 30;
$SQL = "INSERT INTO abc () VALUES('first name', '".$var."')";

2)... or have two different INSERT s for your default values: 2)...或为您的默认值设置两个不同的INSERT

/* for the default value */
$SQL = "INSERT INTO abc (somefield) VALUES('first name')";

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

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