0

In php I have a form like this

<form action="" method="post">
    <table id="user-data">
        <tr>
            <td>Firstname</td>
            <td>Lastname</td>
            <td>Age</td>
        </tr>
        <tr>
            <td><input type="text" name="firstname[]"></td>
            <td><input type="text" name="lastname[]"></td>
            <td><input type="text" name="age[]"></td>
        </tr>
        <tr>
            <td><input type="text" name="firstname[]"></td>
            <td><input type="text" name="lastname[]"></td>
            <td><input type="text" name="age[]"></td>
        </tr>       
    </table>
    <input type="submit" name="submit" value="submit">
</form>

Now I want to insert the data into mysql table. For that my script is like this so far now

<?php
$host = 'localhost';
$uname = 'root';
$pwd  = 'root';
$db = 'Test';
$con=mysqli_connect($host,$uname,$pwd,$db);
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


if(isset($_POST['submit'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
echo '<pre>';
print_r($firstname);
print_r($lastname);
print_r($age);
echo '</pre>';
?>

Now from here I want to know how to insert the values into database within the array. Any help and suggestions will be really apprecaible. Thanks

2
  • If you're not using a framework PDO should be a good choice php.net/manual/en/book.pdo.php Commented Sep 9, 2014 at 12:53
  • You can find your answer here quite nicely explained :) Commented Sep 9, 2014 at 13:05

4 Answers 4

0

Use insert statement, you can see how to do that here :

http://www.w3schools.com/sql/sql_insert.asp

Sign up to request clarification or add additional context in comments.

1 Comment

within array how to insert the data into database that I want to know.
0

Try this one

<?php
if(isset($_POST['submit'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$firstNameVal = $lastNameVal = $ageVal ='';
foreach ($firstname as $fn) {
    $firstNameVal .= $fn;
}
foreach ($lastname as $ln) {
    $lastNameVal .= $ln;
}
foreach ($age as $ag) {
    $ageVal .= $ag;
}
"insert into tablename (firstname,lastname,age) values ($firstNameVal,$lastNameVal,$ageVal)";
}
?>

Comments

0

You want to insert all the firstnames, lastnames and ages into the database? Use foreach on the $firstname variable and go from there.

EDIT:

$numberInArray = count($firstname);
for ($i=0; $i<$numberInArray; $i++) {
   // query with $firstname[$i], $lastname[$i] and $age[$i]
}

This way you will first count the number of variables in the array, and then you will walk through all of the arrays, and insert these values. I hope you can write the INSERT query yourself, using your framework (I would suggest using prepared queries with pdo for example).

Comments

0
if(!empty($_POST)) {
        for($i=0;$i<count($_POST['firstname']);$i++) {
            $firstName = $_POST['firstname'][$i];
            $lastName = $_POST['lastname'][$i];
            $age = $_POST['age'][$i];
            $sql = "INSERT INTO tablename (firstname,lastname,age) values ('".$firstName."','".$lastName."','".$age."')";
        }
    }

Comments

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.