-3

Im using the following

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

which gives me a nice little array as so:

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

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

How does on set the script up to enter into a database. Say i wanted it to enter a joined 'reference number' and then in another field all the test,test2,test3 etc

so id | field

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

   1    |  test2

   1    |  test3

and so on?

Thanks again

D

1
  • 3
    First - read the following -> php.net/manual/en/book.mysql.php have a go at some coding then come back and ask a more specific question Commented Nov 16, 2011 at 11:28

1 Answer 1

-1

Try to use foreach in this case:

<?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);
4
  • 1
    If the OP had included some basic knowledge of MySQL coding within PHP perhaps this would have been a good answer - but i very much suspect the OP wouldnt know what to put before the 2nd line !!!! Commented Nov 16, 2011 at 11:33
  • i blame it on dyslexia im afraid Commented Nov 16, 2011 at 11:48
  • @DavidSpalton you blame what on dyslexia ? you managed to write all of the code in your question .... why does it prevent you from doing some research ? Commented Nov 16, 2011 at 11:51
  • for forgetting what to look for and for getting in a loop being confused. Commented Nov 16, 2011 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.