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:

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

报告相同问题?

悬赏问题

  • ¥50 关于#微信小程序#的问题:微信小程序支持h5实现webrtc h264 h265低延迟传输渲染,能付费帮我们解决一下吗
  • ¥20 请问ansys License manager 这个问题如何解决
  • ¥15 memory.limit()' is no longer supported
  • ¥15 基于stm32f4产生两路pwm信号并可由串口通信调节
  • ¥15 C++/QT设置函数调用条件(多个功能调用同一,且各自调用条件不同)
  • ¥15 UE5样条线生成的网格UV问题
  • ¥15 如何用最短的时间大致看懂springboot+vue的项目
  • ¥15 (有偿)懂数值分析和含时变参数微分方程的来
  • ¥15 abaqus随机生成二维颗粒
  • ¥15 安装ansys许可证管理器时出现了这个问题,如何解决?