为mysql插入准备数组

时间:2017-05-16 14:00:13

标签: php mysql arrays

我的表单中有多个名称为itm的复选框 这显然将所有选中的值存储到数组$ _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>

1 个答案:

答案 0 :(得分:-1)

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

将$ _POST ['itm']数组内嵌到一个新字符串中,用以下字符分隔:

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

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