Php 为mysql插入准备数组

Php 为mysql插入准备数组,php,mysql,arrays,Php,Mysql,Arrays,我有一个表单,它有多个复选框,名称为itm 这显然会将所有选中的值存储到数组$\u POST['itm'][] 我的问题是我希望数组的值以以下格式存储在相关列中 1:88:99:77: :是分隔符,数字是复选框值 但是,当我提交表单时,它只显示Array() 然后我尝试了serialize()函数。。根本不是我想要的 有人能帮忙吗 代码如下`` if (mysqli_connect_errno()) { echo "Failed to connect t

我有一个表单,它有多个复选框,名称为itm 这显然会将所有选中的值存储到数组$\u POST['itm'][]

我的问题是我希望数组的值以以下格式存储在相关列中 1:88:99:77: :是分隔符,数字是复选框值

但是,当我提交表单时,它只显示Array() 然后我尝试了serialize()函数。。根本不是我想要的

有人能帮忙吗

代码如下``

        if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    $forname = $_POST['forename'];
    $surname = $_POST['surname'];
    $newfname = str_replace("'","",$_POST['forename']);
    $newsname = str_replace("'","",$_POST['surname']);
    $year = date('y');
    $month = date('m');
    $day = date('d');
    $username = $newfname.$newsname.$month.$year;


    // Perform queries 
    $addq = mysqli_query($con,"INSERT INTO `workshop-jobs` (wsjid,  wscid,  wsccid, summary,  jobtype,  caterhamwarrantyid,  worktobedone,  quoteinvref,  datearrived,  workstartdate,  workstarttime,  workfinishdate,  hours,  reqbydate,  technician,  status,  completed,  invoiced,  notes) 
    VALUES ('','$_POST[wscid]','$_POST[wsccid]','$_POST[summary]','$_POST[jobtype]','$_POST[caterhamwarrantyid]','$_POST['itm']','$quoteinvref','$_POST[datearrived]','$_POST[workstartdate]','$_POST[workstarttime]','$_POST[workfinishdate]','0','$_POST[reqbydate]','$_POST[technician]','$_POST[status]','0','0','$_POST[notes]')");
    if($addq){
    $last_id = $con->insert_id;
    $lastid = mysqli_query($con,"SELECT * FROM `users` WHERE userid = '$last_id'");
    $lastidr = mysqli_fetch_assoc($lastid);
    header("Location: addjobbat.php?wscid=$last_id");   
    }
    echo mysqli_error($con);
    mysqli_close($con);

表格代码

<div class="panel-body">
                    <input name="itm[]" class="serviceitem" type="checkbox" value="1:">
  &nbsp;&nbsp;1000 / 3 Month Service ( Sigma / Duratec / CSR )<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="2:">
  &nbsp;&nbsp;1000 / 3 Month 620 Service<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="3:">
  &nbsp;&nbsp;Annual Service ONE ( Sigma / Duratec / CSR )<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="4:">
  &nbsp;&nbsp;Annual Service ONE ( Duratec R500 )<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="5:">
  &nbsp;&nbsp;620 Annual Service ONE<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="6:">
  &nbsp;&nbsp;Annual Service TWO ( Sigma / Duratec / CSR )<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="7:">
  &nbsp;&nbsp;620 Annual Service TWO<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="8:">
  &nbsp;&nbsp;4 year add on (Sigma / Duratec / CSR)<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="9:">
  &nbsp;&nbsp;6 year add on (Sigma / Duratec / CSR)<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="10:">
  &nbsp;&nbsp;500 Mile Service ( K-Series )<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="11:">
  &nbsp;&nbsp;3000 Mile Service ( K-Series )<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="12:">
  &nbsp;&nbsp;6000 Mile Service ( K-Series ) <br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="13:">
  &nbsp;&nbsp;12000 Mile Service ( K-Series )<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="14:">
  &nbsp;&nbsp;24000 Mile Service ( K-Series )<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="15:">
  &nbsp;&nbsp;Race Car Service ( All Models )<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="16:">
  &nbsp;&nbsp;Suzuki 160 1st Service<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="17:">
  &nbsp;&nbsp;Suzuki 2 Year Addon<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="18:">
  &nbsp;&nbsp;Suzuki 3000 Mile Service<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="19:">
  &nbsp;&nbsp;Suzuki 4 Year Addon<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="20:">
  &nbsp;&nbsp;Suzuki 6000 Mile Service<br>
                    <input name="itm[]" class="serviceitem" type="checkbox" value="21:">
  &nbsp;&nbsp;Suzuki 6 Year Addon<br>
                  </div>

1000/3个月服务(Sigma/Duratec/CSR)
1000/3个月620次保养
年度服务一(Sigma/Duratec/CSR)
年度服务一(Duratec R500)
620年服务一期
年度服务二(Sigma/Duratec/CSR)
620年度服务二期
4年附加(西格玛/杜拉特克/企业社会责任)
六年附加(Sigma/Duratec/CSR)
500英里服务(K系列)
3000英里服务(K系列)
6000英里服务(K系列)
12000英里服务(K系列)
24000英里服务(K系列)
赛车服务(所有车型)
铃木160第一次服务
铃木2年插件
铃木3000英里服务
铃木4年插件
铃木6000英里服务
铃木6年插件

为什么不创建一个要插入的新变量

将$_POST['itm']数组内爆为一个新字符串,由以下内容分隔:

$insertThis = implode(":",$_POST['itm']);

内爆将数组拆分为字符串,用第一个参数分隔每个元素,第二个参数是数组

首先,在放置
echo mysqli_error($con)时,您不会看到可能的错误是否应该有,因为在您看到之前,标头正在重定向。您还应该编辑您的帖子,以包含用于此目的的HTML(表单),因为我们不知道复选框是否被视为数组。您的代码也倾向于sql注入;使用准备好的语句。关于sql注入。。它在一台本地机器上,只有我和另外两个人可以访问这个系统,所以不太麻烦;你回答了一些与这里可能出现的问题无关的问题。无论如何;您已经得到了答案,请看。您的脚本可能会遇到这样的风险:即使使用存储这样的数据,也会发生什么情况,即
val:val:val
在一列中会让您陷入地狱。考虑一下如何以这种格式查询数据。这就是地狱,在我的代码里,它到底会去哪里?这正是我在insert语句之前想要做的,在变量声明下面,在这一行中显示错误:对于($x=0;$x try,我刚刚输入的新代码,如果$_POST['itm']存在,代码将正常工作。太棒了!知道内爆会有帮助,但有心理障碍lol非常感谢