我有一张这样的桌子:
reqnumber empname
1 name1
2 name2
请注意,reqnumber
是主键,它会为每个插入自动递增,我想在其上添加前缀,然后添加数字。这是一个示例:
reqnumber empname
TS1 name1
TS2 name2
我不太确定从哪里开始,因为我的插入代码上没有reqnumber
,我已经阅读并尝试了一些方法,但是我无法真正将其合并到我的插入代码中。另外,我使用的是动态表单,这会使我的插入代码更加复杂。
这是我的插入代码:
$connect = mysqli_connect("localhost", "root", "", "pcrequest");
if(isset($_POST["reqname"]))
{
$length = 3;
$trackingcode = 'TS-' . substr(str_shuffle("0123456789"), 0, $length);
$req_date = $_POST["req_date"];
$reqname = $_POST["reqname"];
$date = $_POST["date"];
$empname = $_POST["empname"];
$position = $_POST["position"];
$account = $_POST["account"];
$platform = $_POST["platform"];
$processor = $_POST["processor"];
$ram = $_POST["ram"];
$monitor = $_POST["monitor"];
$phone = $_POST["phone"];
$phonetype = $_POST["phonetype"];
$headset = $_POST["headset"];
$req_table = $_POST["req_table"];
$req_chair = $_POST["req_chair"];
$approval = $_POST["approval"];
$status = $_POST["status"];
$query = '';
for($count = 0; $count<count($reqname); $count++)
{
$req_date_clean = mysqli_real_escape_string($connect, $req_date[$count]);
$reqname_clean = mysqli_real_escape_string($connect, $reqname[$count]);
$date_clean = mysqli_real_escape_string($connect, $date[$count]);
$empname_clean = mysqli_real_escape_string($connect, $empname[$count]);
$position_clean = mysqli_real_escape_string($connect, $position[$count]);
$account_clean = mysqli_real_escape_string($connect, $account[$count]);
$platform_clean = mysqli_real_escape_string($connect, $platform[$count]);
$processor_clean = mysqli_real_escape_string($connect, $processor[$count]);
$ram_clean = mysqli_real_escape_string($connect, $ram[$count]);
$monitor_clean = mysqli_real_escape_string($connect, $monitor[$count]);
$phone_clean = mysqli_real_escape_string($connect, $phone[$count]);
$phonetype_clean = mysqli_real_escape_string($connect, $phonetype[$count]);
$headset_clean = mysqli_real_escape_string($connect, $headset[$count]);
$req_table_clean = mysqli_real_escape_string($connect, $req_table[$count]);
$req_chair_clean = mysqli_real_escape_string($connect, $req_chair[$count]);
$approval_clean = mysqli_real_escape_string($connect, $approval[$count]);
$status_clean = mysqli_real_escape_string($connect, $status[$count]);
if($req_date_clean != '' && $reqname_clean != '' && $date_clean != '' && $empname_clean != '' && $position_clean != '' && $account_clean != '' && $platform_clean != '' && $processor_clean != '' && $ram_clean != '' && $monitor_clean != '' && $phone_clean != '' && $phonetype_clean != '' && $headset_clean != '' && $req_table_clean != '' && $req_chair_clean != '' && $approval_clean != '' && $status_clean != '')
{
$query .= '
INSERT INTO request(trackingcode, req_date, reqname, date, empname, position, account, platform, processor, ram, monitor, phone, phonetype, headset, req_table, req_chair, approval, status)
VALUES("'.$trackingcode.'", "'.$req_date_clean.'", "'.$reqname_clean.'", "'.$date_clean.'", "'.$empname_clean.'", "'.$position_clean.'", "'.$account_clean.'", "'.$platform_clean.'", "'.$processor_clean.'", "'.$ram_clean.'", "'.$monitor_clean.'", "'.$phone_clean.'", "'.$phonetype_clean.'", "'.$headset_clean.'", "'.$req_table_clean.'", "'.$req_chair_clean.'", "'.$approval_clean.'", "'.$status_clean.'");
';
}
答案 0 :(得分:0)
由于您的列使用AUTO_INCREMENT,因此必须为整数或浮点类型(任何大小)。 https://dev.mysql.com/doc/refman/5.7/en/create-table.html
某些属性并不适用于所有数据类型。 AUTO_INCREMENT适用 仅适用于整数和浮点类型。
因此您无法为其分配字符串值。 您可以添加一个新列,并使用触发器来填充它,以生成并插入所需的值(内容将在插入时生成)。 或者您可以创建一个视图来访问它(每次查询时都会对内容进行评估)。