MySql用户输入如果为空= '00',则ELEIF单个数字先添加零

时间:2021-01-01 22:09:20

I have a user input that if they leave it blank it will POST '00' else it POSTS whatever they entered. I need to set it up as if blank post '00' elseif user enters single digit it will append a '0' at the beginning. I already have the database set up as an INT with ZEROFILL so that it will hold the zero. My Code as of now is

我有一个用户输入,如果他们把它留空,它将POST'00',否则它就是他们输入的东西。我需要设置它,好像空白的帖子'00',如果用户输入单个数字,它将在开头附加一个'0'。我已经将数据库设置为带有ZEROFILL的INT,以便它保持零。我的代码截至目前为止

        if (empty($_POST['num'][$i]))
        { $_POST['num'][$i] = '00';}

and to insert it into the table I run

并将其插入我运行的表中

        $datanum = "Insert into Data (num) VALUES ('" . $_POST['num'][$i] . "')";

1 个解决方案

#1


0  

Use str_pad function

使用str_pad函数

$_POST['num'][$i] = str_pad(trim($_POST['num'][$i]), 2, "0", STR_PAD_LEFT);

Execute the code here http://sandbox.onlinephpfunctions.com/code/be5f1e4888fefd5522a9e83274cc587c878e1f8c

在这里执行代码http://sandbox.onlinephpfunctions.com/code/be5f1e4888fefd5522a9e83274cc587c878e1f8c

#1


0  

Use str_pad function

使用str_pad函数

$_POST['num'][$i] = str_pad(trim($_POST['num'][$i]), 2, "0", STR_PAD_LEFT);

Execute the code here http://sandbox.onlinephpfunctions.com/code/be5f1e4888fefd5522a9e83274cc587c878e1f8c

在这里执行代码http://sandbox.onlinephpfunctions.com/code/be5f1e4888fefd5522a9e83274cc587c878e1f8c