【问题标题】:PHP Array, adding to MYSQLPHP数组,添加到MYSQL
【发布时间】:2011-12-30 08:31:00
【问题描述】:

我正在使用以下内容

<?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 等

所以 编号 |字段

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

   1    |  test2

   1    |  test3

等等?

再次感谢

D

【问题讨论】:

标签: php mysql arrays


【解决方案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);

【讨论】:

  • 如果 OP 在 PHP 中包含一些 MySQL 编码的基本知识,也许这将是一个很好的答案 - 但我非常怀疑 OP 不知道在第二行之前放什么!!!!
  • 恐怕我把它归咎于阅读障碍
  • @DavidSpalton 你把阅读障碍归咎于什么?您设法编写了问题中的所有代码....为什么它会阻止您进行一些研究?
  • 因为忘记了要查找的内容并陷入混乱。
猜你喜欢
  • 2011-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-11
  • 2015-04-08
  • 1970-01-01
相关资源
最近更新 更多