简体   繁体   中英

php save data into database within an array

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

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

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

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).

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)";
}
?>
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."')";
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM