简体   繁体   English

PHP:在mysqli prepare中插入NULL值

[英]PHP: Insert NULL value in mysqli prepare

I am in a strange situation. 我处在一个奇怪的情况。 In my database table fields are defined as NOT NULL . 在我的数据库表中,字段定义为NOT NULL When i run below query it doesn't show the output of echo 'Data Added'; 当我在查询下面运行时,它不显示echo 'Data Added';的输出echo 'Data Added';

But if I change field defintions via phpmyadmin to NULL=yes then it is showing Data Added . 但是,如果我将通过phpmyadmin的字段定义更改为NULL=yes那么它显示的是Data Added

$results = $mysqli->prepare("INSERT INTO posting (emp_id, title, open, description, keywords, min, max, ip_add) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$results ->bind_param("isisssss", $emp_id, $title, $open, $description, $keywords, $min, $max, $ip_add );

$results->execute();

if($results->affected_rows == 1){

            echo 'Data Added';
        }

I want to know do i need to change all field in mysql table and make them Null = Yes or is there any other way to achieve this? 我想知道我是否需要更改mysql表中的所有字段并使它们为Null =是,还是有其他方法可以实现此目的?

Am i doing something wrong here. 我在这里做错什么了吗? please advise. 请指教。

When the column in PHPmyAdmin is set 'not null' it means that you need to have some value in that field, otherwise it fails, so in short your INSERT query would fail and you won't see echo 'Data Added'; 当PHPmyAdmin中的列设置为“ not null”时,这意味着您需要在该字段中具有一些值,否则它将失败,因此简而言之,您的INSERT查询将失败,并且您将不会看到echo 'Data Added'; since no rows were affected due to the previous error in MySQL. 因为没有行由于MySQL先前的错误而受到影响。

As to what all you should make NULL depends on your setup, some fields might be mandatory, for example you may want your 'emp_id' to be present all times in a record so you would make it 'NOT NULL'. 至于您应该使NULL做什么取决于您的设置,某些字段可能是强制性的,例如,您可能希望您的'emp_id'在记录中始终存在,因此您将其设为'NOT NULL'。

Set the field attributes and make changes in your code accordingly, if a field is mandatory in the database you can take measures to ensure that the field is present before being sent to the database. 设置字段属性并相应地更改代码,如果数据库中必须包含字段,则可以采取措施以确保在将字段发送到数据库之前该字段存在。

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

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