Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

how can I insert this array into my database? supposing I already have a database?

<?php

if(isset($_POST['myname'], $_POST['myage'], $_POST['mygen']) === true){

//this is the code where I set the keys and values

$array = array($_POST['myname'], $_POST['myage'], $_POST['mygen']);

foreach ($array as $i => $values) {
    print "<pre>$i {\n";
    foreach ($values as $key => $value) {
        print "    $key => $value\n";
    }
    print "}\n</pre>";
}
}




?>
<html>
    <head>
    <script type="text/javascript" >
function CreateTextbox() 

//I generate textbox dynamically by this code

{
createTextbox.innerHTML = createTextbox.innerHTML +"Name: <input type='text' name='myname[]' /><br>"
+"Age: <input type='text' name='myage[]' /><br>"
+"Gender: <input type='text' name='mygen[]' /><br>" 
}
    </script>
    </head>
    <body> 
        <form action="" method="post">
            <div id="createTextbox"></div>
            <input type="button" value="clickHere" onClick="CreateTextbox()" />
            <input type="submit" value="Submit" name="submit" />
        </form>
    </body>
</html>

please help :(

share|improve this question
 
What have you tried? –  Waleed Khan Jul 18 '12 at 17:57
 
serialize and unserialize? –  Laxus Jul 18 '12 at 17:58
2  
Do you want to store the array itself in the database or do you want to store the values in the array into individual database fields? –  Mike Brant Jul 18 '12 at 17:59
 
Also, what database? Some databases actually have array types. –  Mike Christensen Jul 18 '12 at 18:01
 
@MikeBrant i want to store the array into individual database fields, for example i want this particular array to be stored in different rows in the database, im using phpmyadmin BTW 0 { 0 => Jake 1 => Sheryl } 1 { 0 => 20 1 => 19 } 2 { 0 => male 1 => female } –  JakeWevDeb Jul 18 '12 at 18:42
show 6 more comments

1 Answer

up vote 0 down vote accepted

You could need to count the amount of elements in the incoming post arrays like this

$data_count = count($_POST['myname']);

You may also consider here adding some errro handling to verify the size of all the post array are the same, but I will skip that.

Then you want to loop through the arrays and do your DB inserts like this:

for ($i=0; $i<$data_count; $i++) {
    $name = $_POST['myname'][$i];
    $age = $_POST['myage'][$i];
    $gen = $_POST['mygen'][$i];
    // clean up data for DB insert here 
    // make DB insert
}

Note that I have left out the actual code for cleaning the data and inserting the data, as thiat is kind of out of the scope of this discussion.

share|improve this answer
 
thanks you sir! I've been experimenting on the codes for days until I asked here :D thank you :D –  JakeWevDeb Jul 18 '12 at 19:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.