0

我需要通过一次选择在我的数据库中插入两个值。

我的表格:

              <form action="updateresult.php" method="post" name="edit">         
<div class="row">
    <div class="content">              
        <div class="pull-left">             
          <select name="eplayer_id[]" class="tripObject">
            <option value="%">Select</option>
          <?php 
        $lastteam = "";
do { 
?>
        <?php if ($lastteam != $row_datagoal['tteam'])
    echo '<optgroup label="'.$row_datagoal['name'].'">';
        echo '<option value="'.$row_datagoal['id'].','.$row_datagoal['tteam'].'">'.$row_datagoal['playerName'].'</option>';
        if ($lastteam != $row_datagoal['tteam'])
    echo '</optgroup>';
        $lastteam = $row_datagoal['tteam'];     
        ?>
        <?php
} while ($row_datagoal = mysql_fetch_assoc($datagoal));
  $rows = mysql_num_rows($datagoal);
  if($rows > 0) {
      mysql_data_seek($datagoal, 0);
      $row_datagoal = mysql_fetch_assoc($datagoal);
  }  
?>
          </select>      
          <input class="inputfieldres" name="ecount[]" type="text" value="" size="2" maxlength="2" />
          <input type="hidden" name="ematch_id[]" value="<?php echo $row_dataresults['id']; ?>" />
      </div>
    </div>
</div>
<a href="#" id="Add">ADD MORE SELECT</a>
<input class="btnresults" type="submit"  value="Save" />
<input type="hidden" name="MM_insert" value="form1" />   
</form>

用户可以在提交表单之前添加更多选择。为了做到这一点,我使用了以下脚本来克隆表单中的项目:

 $(document).ready(function(){
    $("#Add").click(function(){
        var obj =  $("div.content").eq(0).clone(); //this will clone the html elements
        obj.append('<div class="pull-left"><a href="#"onclick="removeDOM(this)"><img src="images/p6_delete.gif" width="14" height="14" alt="Remove" /></a></div>');
        $("div.row").append(obj); //this will append to the existing elements
    });
});

function removeDOM(thisObj){
        $(thisObj).parent().parent().remove();
    }

要将数据插入我的 db 表中,我尝试了以下查询:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")){  
     $newteam = explode (",",$_POST['eplayer_id']);         
for($j=0,$len=count($newteam[0]);$j<$len;$j++){                
  $insertSQL = sprintf("INSERT INTO f_matchevents (eplayer_id, eteam_id, ematch_id, ecount) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString(trim($newteam[0] [$j]), "int"),
                       GetSQLValueString(trim($newteam[1] [$j]), "int"),
                       GetSQLValueString($_POST['ematch_id'] [$j], "int"),
                       GetSQLValueString($_POST['ecount'] [$j], "int"));
  mysql_select_db($database_config, $config);
  $Result1 = mysql_query($insertSQL, $config) or die(mysql_error());} 
$insertGoTo = "player_list.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

提交后我收到以下错误:

Warning: explode() expects parameter 2 to be string, array given in C:\xampp\htdocs\*****\updateresult.php on line 43

在此先感谢您的帮助。

4

1 回答 1

1

不需要使用explode(),因为你已经有了数组。

所以可以直接使用

$_POST['eplayer_id'][0]
$_POST['eplayer_id'][1]

根据您的要求。

于 2013-07-21T12:46:14.890 回答