donglie1994 2015-07-14 13:17
浏览 48
已采纳

使用PHP将表单数组添加到MySQL

I cannot get the array put into the MySQL database in the format 'arrayitem1,arrayitem2,etc...'

Here's where my forms are:

<form action="http://www.nccskills.co.uk/bookings/process.php" method="post" name="bookingForm">
  <div id="row12">
    <div class="col1">
      <input type="checkbox" name="vocCheck[]" value="HASA">
      &nbsp;&nbsp;HEALTH AND SAFETY AWARENESS
    </div>
    <div class="col2">
      <input type="checkbox" name="vocCheck[]" value="EAD">
      &nbsp;&nbsp;EQUALITY AND DIVERSITY
    </div>
  </div>
  <div id="row13">
    <div class="col1">
      <input type="checkbox" name="vocCheck[]" value="ECC">
      &nbsp;&nbsp;EFFECTIVE CUSTOMER CARE
    </div>
    <div class="col2">
      <input type="checkbox" name="vocCheck[]" value="CC">
      &nbsp;&nbsp;CARE CERTIFICATE</div>
    </div>

What php would I use to achieve this?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duandaijian1583 2015-07-14 13:41
    关注

    In your process.php file you would want to have code that looks somewhat like this:

    //Create a connection using MySQLi
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    //Prepare a statement.
    $conn->prepare('INSERT INTO tablename(fieldname) VALUES(?)');
    
    //Put all the checkboxes together, if anything is selected.
    $checked = '';
    if(is_array($_POST['vocCheck'])) {
        $checked = implode(',', $_POST['vocCheck']);
    }
    
    //Bind the value.
    $conn->bind_param('s', $checked);
    
    //Execute the question.
    $conn->execute();
    

    Not 100% sure you actually need to check if $_POST['vocCheck'] is an array and I cant try right now so I included it just to be on the safe side. You might want to throw some error handling into that in case the connection doesn't work etc. Some useful links for you:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵