Question

basically i am trying to imput two arrays in the database but i can't seem to actually get them in the right tables.

here is the code for the php function:

$numecap1 = $_POST['numecap'];
$contentcap1 = $_POST['contentcap'];
$numecap = implode(" ", $numecap1);
$contentcap = implode(" ", $contentcap1);   
$count_name = count($_POST['numecap']);
for($i=0;$i<$count_name ;$i++){
$_numecap  = mysqli_escape_string($con,$numecap[$i]);
$_contentcap  = mysqli_escape_string($con, $contentcap[$i]);

$sql3 = "INSERT INTO `".$prefix."".$titlu."`(numecap, contentcap) VALUES ('$numecap', '$contentcap')";}

and here is the html form (note: the java script adds how many text labales i need):

<form action="" method="post">
    Nume table <input type="text" name="table"><br>
    Autor <input type="text" name="autor"><br>
    Nrcap <input type="text" name="cap"><br>
    <input type="button" onclick="addInput()"/>

        <span id="responce"></span>
        <script>
            var boxName2 = 0;
            var boxName = 0;
            function addInput()
            {
                 var boxName="numecap"; 
            document.getElementById('responce').innerHTML+='<br/>Nume cap<input type="text"  name="'+boxName+'[]" "  /><br/>';
                 var boxName2="contentcap"; 
            document.getElementById('responce').innerHTML+='<br/>Continut cap<input type="text"  name="'+boxName2+'[]" "  /><br/>';


            }
        </script>   
    <input type="submit" name="SubmitButton"/>
</form>   

If someone can help, itll be hghly appreciated, since i am desperate!

Était-ce utile?

La solution

change your $sql to this:

$sql3 = "INSERT INTO `".$prefix."".$titlu."`(numecap, contentcap) VALUES ('$numecap[$i]', '$contentcap[$i]')";

because that way, you create a new row, insert only one value. create a new row, insert next value. And so on.

Your for loop should look like this:

for($i=0;$i<$count_name ;$i++){
$_numecap  = mysqli_escape_string($con,$numecap[$i]);
$_contentcap  = mysqli_escape_string($con, $contentcap[$i]);

$sql3 = "INSERT INTO `".$prefix."".$titlu."`(numecap, contentcap) VALUES ('$_numecap', '$_contentcap')";

mysqli_query($c, $sql3);
}

Autres conseils

Perhpas you should look into the seralize and unseralize function which is available in PHP, this will assist you in inserting information into the database in the form of an Array!

$Array = array(1,2,3,4,5,6,7,8);
$Array_2 = array(1,43985);

$Array = seralize($Array);
$Array_2 = seralize($Array_2);

http://uk3.php.net/manual/en/function.serialize.php

http://uk3.php.net/manual/en/function.unserialize.php

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top