PHP数组,添加到MYSQL

时间:2011-11-16 11:25:00

标签: php mysql arrays

我使用以下

<?php
$vipInfo = $_POST['vipInfo'];
echo "<pre>";
print_r($vipInfo);
echo "</pre>";

$result = sizeof($vipInfo,0);

  foreach($vipInfo as $key => $value){
    echo "Form ID: $key, Event: $value <br />";


?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"  "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript"> 

    var nFloor = "";    

    function removeField(nField){

    nField.parentNode.parentNode.removeChild(nField.parentNode);
}

function insertField(){

    var newFieldContainer = document.createElement('div');
    var newFieldLabel = document.createElement('label');
    newFieldLabel.innerHTML = "Event:&nbsp;&nbsp;&nbsp;";       
    var newField = document.createElement('input');
    newField.type = "text";
    newField.name = "vipInfo[]";
    newFieldContainer.appendChild(newFieldLabel);
    newFieldLabel.appendChild(newField);
    var deleteBtn = document.createElement('input');
    deleteBtn.type = "button";
    deleteBtn.value = "Remove";
    deleteBtn.style.marginLeft = "5px";
    deleteBtn.onclick = function(){removeField(this)};
    newFieldContainer.appendChild(deleteBtn);
    document.forms[0].insertBefore(newFieldContainer,nFloor);
}

function init(){

    var insertBtn = document.getElementById('newFieldBtn')
    insertBtn.onclick = function()
        {
         insertField();
        }
    nFloor = insertBtn;     
}

navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);    

</script>
</head>
    <body>
        <form action="process.php" method="post">

            <div class="field"><label>Event:&nbsp;&nbsp;&nbsp;<input type="text" name="vipInfo[]"></label></div>

        <input type="button" id="newFieldBtn" value="New Field"> 
        <input type="submit" name="submit" value="Submit">

    </form>
</body>

这给了我一个很好的小数组:

Array
(
    [0] => test1
    [1] => test2
    [2] => test3

)
Form ID: 0, Event: test1 
Form ID: 1, Event: test2 
Form ID: 2, Event: test3 

如何设置脚本以进入数据库。 说我希望它输入一个加入的'参考号',然后在另一个字段中输入所有测试,test2,test3等

所以        id |字段

   --------------
   1    |  test

   1    |  test2

   1    |  test3

等等?

再次感谢

d

1 个答案:

答案 0 :(得分:-1)

在这种情况下尝试使用foreach

<?php
$sql = "INSERT INTO table (id, event) VALUES ";
foreach($array AS $id => $event) {
  $sql .= " ('".$id."','".$event."'),";
}
$sql = substr($sql,-1); //Remove the last ,

mysql_query($query);