1

i'm a beginner on php and i'm trying to save this loop to a database but it only saves the last row on the database..i hope someone can answer this thanks in advance.

<?php
    $child = $_REQUEST['NumberofChildren'];//requested from the lastpage

    echo "<form method='post' action='familysave.php'>";

    for($n=1; $n<=$child; $n++)

    {
        echo "<table>
        <tr>
        <td><input type='text' name='sibname' placeholder='Fullname' style='width: 150px;' required></td>
        <td><input type='text' name='sibage' placeholder='Age' style='width: 35px;' required></td>
        <td><input type='text' name='sibhea' placeholder='Highest Educational Attainment' style='width: 260px;'></td>
        <td><input type='text' name='sibcs' placeholder='Civil Status' style='width: 100px;' required></td>
        <td><input type='text' name='siboccu' placeholder='Occupation' style='width: 100px;' required></td>
        </tr>       

        </table>";
    }
    ?>
    <input type="submit" value="Save">
    <?php echo "</form>"; 
    ?>

this is the code of familysave.php

    <?php

    $conn = mysql_connect($host, $username, $password);

        if(! $conn)
        {
            die('Could not connect: ' . mysql_error());
        }
    $sibname =$_POST['sibname']
    $sibage =$_POST['sibage']
    $sibhea =$_POST['sibhea']
    $sibcs =$_POST['sibcs']
    $siboccu =$_POST['siboccu']

    $sql = "INSERT INTO $tblname (NameofSiblings, Age, HEA, CivilStatus, Occupation) VALUES     ('$sibname', '$sibage', '$sibhea', '$sibcs', '$siboccu')";
    mysql_select_db($dbname);
    $retval = mysql_query($sql, $conn);

    if(! $retval)
    {
        die('Could not Enter Data:' . mysql_error());
    }

    mysql_close($conn);

    ?>

1 Answers1

0

All your FORM fields have same names even when you repeat the fieldset in a loop. Update those names to look like name='sibname[]' and then in PHP run a loop on all of those to grab the values.

Like

Step1:

for($n=1; $n<=$child; $n++)
{
    echo "<table>
    <tr>
    <td><input type='text' name='sibname[]' placeholder='Fullname' style='width: 150px;' required></td>
    <td><input type='text' name='sibage[]' placeholder='Age' style='width: 35px;' required></td>
    <td><input type='text' name='sibhea[]' placeholder='Highest Educational Attainment' style='width: 260px;'></td>
    <td><input type='text' name='sibcs[]' placeholder='Civil Status' style='width: 100px;' required></td>
    <td><input type='text' name='siboccu[]' placeholder='Occupation' style='width: 100px;' required></td>
    </tr>       
    </table>";
}

Step2:

Add a hidden field to your form to indicate the number of children added

<input type="hidden" name="n" value="<?php  echo $child; ?>">

Step 3:

Now run a loop to grab the values

$n=intval($_POST["n"]);
for($i=0;$i<$n;$i++)
{
  $sibname =$_POST['sibname'][$i];    // better to use isset() as well
  $sibage =$_POST['sibage'][$i];
  $sibhea =$_POST['sibhea'][$i];
  $sibcs =$_POST['sibcs'][$i];
  $siboccu =$_POST['siboccu'][$i];
  // Now save these values but do not use the deprecated MySQL API, see the link below.
}

Also go through this: How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • On step 1 please put it as it is, that $n is only for step 3 – Hanky Panky Mar 09 '14 at 07:46
  • For sql part please read this http://us.php.net/manual/en/mysqlinfo.api.choosing.php – Hanky Panky Mar 09 '14 at 07:47
  • thanks it works!,.but how can i save it in only 1 row? – user3397568 Mar 09 '14 at 07:51
  • That will be a very very bad database design if you have to save multiple people's records in one row, would never advise you to learn that. One row for every unique record is good. However just for educational purposes you can create some strings by using string concatenation in the loop and then run the query only once – Hanky Panky Mar 09 '14 at 07:54
  • can you give me an example how to do it using the variables above? – user3397568 Mar 09 '14 at 09:54