MySQL的不给空值NULL

时间:2018-11-16 21:55:18

标签: php mysql yii2

在我的mysql中,如果我将标题输入保留为空并将数据保存到表中一段时间​​,则将字段保留为空,但不将NULL作为值,我将默认值设置为NULL,但仍将字段保留为空而不是写入NULL。我不知道为什么;如果标题或文本输入在插入数据时为空,我想给字段NULL 在图像中,我显示如果第二张图像中的字段为空,则表被设置为NULL,这表明某个时间不提供NULL。因为在我的php中,我将值设置为NULL来显示<p>No title</p>,但是在这种情况下,我会得到空的<p><p> enter image description here

enter image description here

-插入代码(是Yii2框架)

public static function CreatePost ($MPEMBED)
    {
        $info = Embed::create( $MPEMBED->post_url );

        $urlImage = $info->image;

        $MPEMBED->post_urlTitle = $info->title;
        $MPEMBED->post_urlProvider = $info->providerIcon;
        $MPEMBED->post_urlCode = $info->code;
        $MPEMBED->post_urlPreview = $urlImage;
        $MPEMBED->filestype_id = 100;
        $mediaName = AxValuePost::FileName();
        $mediaFolder = $MPEMBED->post_mediafolder;

        if ($info !== NULL){
            mkdir($mediaFolder, 0777, true);

            $data = file_get_contents($urlImage);
            $new = $mediaFolder . $mediaName .rand().'png';
            $MEDIA = file_put_contents($new, $data);

            if ($MEDIA !== NULL)
            {
                // To upload to DB (TO PASS A DATA TO VALUE TO MYSQL EXP )
                $MPEMBED->post_media = $new;
            }   
        }
        $MPEMBED->save()
    }

-输出代码

public static function PostLink($post, $cssClass)
    {
        if ($post->post_title == NULL ){
            return Html::a(Yii::t('app', $post->post_urlTitle), ['/content/post', 'chn' => $post->channel->channel_title, 'pst' => $post->post_uid], ['class'=> $cssClass ]);
        }else{
            return Html::a(Yii::t('app', $post->post_title), ['/content/post', 'chn' => $post->channel->channel_title, 'pst' => $post->post_uid], ['class'=> $cssClass ]);
        }
    }

1 个答案:

答案 0 :(得分:0)

我通过使用empty()解决了这个问题; if ($post->post_title == NULL )

if ($post->post_title == NULL || empty($post->post_title))

已更改:

public static function PostLink($post, $cssClass)
    {
        if ($post->post_title == NULL ){
            return Html::a(Yii::t('app', $post->post_urlTitle), ['/content/post', 'chn' => $post->channel->channel_title, 'pst' => $post->post_uid], ['class'=> $cssClass ]);
        }else{
            return Html::a(Yii::t('app', $post->post_title), ['/content/post', 'chn' => $post->channel->channel_title, 'pst' => $post->post_uid], ['class'=> $cssClass ]);
        }
    }

至:

public static function PostLink($post, $cssClass)
        {
            if ($post->post_title == NULL || empty($post->post_title)){
                return Html::a(Yii::t('app', $post->post_urlTitle), ['/content/post', 'chn' => $post->channel->channel_title, 'pst' => $post->post_uid], ['class'=> $cssClass ]);
            }else{
                return Html::a(Yii::t('app', $post->post_title), ['/content/post', 'chn' => $post->channel->channel_title, 'pst' => $post->post_uid], ['class'=> $cssClass ]);
            }
        }