php error implode传递的参数无效

时间:2017-08-23 13:22:41

标签: php mysql

我知道这个问题已多次提出,我真的尝试了所有可能的解决方案。但是,我仍然无法弄清楚为什么我会收到此错误。请帮助我真的卡住了。

这是代码:

<div class="dropdown col-md-6 c-margin-b-20" data-control="checkbox-dropdown"  >
    <label class="dropdown-label">Select</label>
    <div class="dropdown-list" style="z-index: 9999;margin-bottom: 20px;"> <a href="#" data-toggle="check-all" class="dropdown-option"> Check All </a>
        <label class="dropdown-option">
            <input type="checkbox" name="ids[]" value="Phoenix Mall,Banglore"/>
            Phoenix Mall,Banglore </label>
        <label class="dropdown-option">
            <input type="checkbox" name="ids[]" value="Orion Mall,Banglore" />
            Orion Mall,Banglore </label>
        <label class="dropdown-option">
            <input type="checkbox" name="ids[]" value=" Lido Mall,Banglore" />
            Lido Mall,Banglore </label>
        <label class="dropdown-option">
            <input type="checkbox" name="ids[]" value=" Ambiance Mall, Delhi"/>
            Ambiance Mall, Delhi </label>
    </div>
</div>
</div>
<?php
$link = mysqli_connect("localhost", "root", "", "midata");
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
$ids = implode(",",$_POST["ids"]);
$camp_name = mysqli_real_escape_string($link, $_REQUEST['camp_name']);
$start_date = mysqli_real_escape_string($link, $_REQUEST['start_date']);
$end_date = mysqli_real_escape_string($link, $_REQUEST['end_date']);
//$store= mysqli_real_escape_string($link, $_REQUEST['store']);
$description = mysqli_real_escape_string($link, $_REQUEST['description']);
if (isset($_POST['submit_val'])) {
    if  ($_POST['elements'] && $_POST['quantity']){
        foreach($_POST['elements'] as $elements){
            foreach($_POST['quantity'] as $quantity){
                $sql = "INSERT INTO create_campaign (camp_name,start_date,end_date,elements, quantity,description) VALUES ( '$camp_name','$start_date', '$end_date',$elements','$quantity', '$description')";
                if ($link->query($sql) === TRUE) {
                } else {
                }
            }
        }
    }
}
?>

请让我知道做错了什么,以便我能弄明白。

2 个答案:

答案 0 :(得分:0)

我建议你在检查我们真正发布的值后,将尝试访问POST数据的行$ids = implode(",",$_POST["ids"]);移动到 if语句块

if (isset($_POST['submit_val'])) { //if $_REQUEST['METHOD']=='POST'
   $ids = implode(",",$_POST["ids"]);
   $camp_name = mysqli_real_escape_string($link, 
   $_REQUEST['camp_name']);
   $start_date = mysqli_real_escape_string($link, 
   $_REQUEST['start_date']);
   $end_date = mysqli_real_escape_string($link, 
   $_REQUEST['end_date']);
  //$store= mysqli_real_escape_string($link, $_REQUEST['store']);
  $description = mysqli_real_escape_string($link, 
  $_REQUEST['description']);
  foreach($_POST['elements'] as $elements){
  //inner code
  }
}

答案 1 :(得分:0)

这肯定会奏效。

<div class="dropdown col-md-6 c-margin-b-20" data-control="checkbox-dropdown"  >
        <label class="dropdown-label">Select</label>

        <div class="dropdown-list" style="z-index: 9999;margin-bottom: 20px;">
            <a href="#" data-toggle="check-all" class="dropdown-option">
                Check All  
            </a>

            <label class="dropdown-option">
                <input type="checkbox" name="store[]" value="Phoenix Mall,Banglore"/>
               Phoenix Mall,Banglore
            </label>

            <label class="dropdown-option">
                <input type="checkbox" name="store[]" value="Orion Mall,Banglore" />
                Orion Mall,Banglore
            </label>

            <label class="dropdown-option">
                <input type="checkbox" name="store[]" value=" Lido Mall,Banglore" />
              Lido Mall,Banglore
            </label>

            <label class="dropdown-option">
                <input type="checkbox" name="store[]" value=" Ambiance Mall, Delhi"/>
                Ambiance Mall, Delhi
            </label>


        </div>
        </div>

    </div>
<br> <br><br>

  <?php

  /* Attempt MySQL server connection. Assuming you are running MySQL
  server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "midata");
 // Check connection
  if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
  }
      $checkBox = implode(',', $_POST['store']);
      $camp_name = mysqli_real_escape_string($link, $_REQUEST['camp_name']);
      $start_date = mysqli_real_escape_string($link, $_REQUEST['start_date']);
      $end_date = mysqli_real_escape_string($link, $_REQUEST['end_date']);


      $description = mysqli_real_escape_string($link, $_REQUEST['description']);


      if (isset($_POST['submit_val'])) {
        if  ($_POST['elements'] && $_POST['quantity']){
          foreach($_POST['elements'] as $elements){
           foreach($_POST['quantity'] as $quantity){

             $sql = "INSERT INTO create_campaign (camp_name,start_date,end_date,store,elements, quantity,description) VALUES ( '$camp_name','$start_date', '$end_date','" . $checkBox . "', '$elements','$quantity', '$description')";
            if ($link->query($sql) === TRUE) {
             } else {


            }


           }
         }
      }

    }


?>