for each loop post only one record with php

Felicity Okache

good evening everyone, i have a for each loop in php that post only one record to database table. my html code below;

<html>
<input type="hidden" name="username[]" value="<?php echo $userRow['username']; ?>" readonly />
<input type="hidden" name="school[]" value="<?php echo $userRow['school']; ?>" readonly />
<SELECT name='cand_name[]' class='form-control'>
<option></option>
<option>Ahmed MUSA</option>
<option>Ahmed Arinze</option>
<option>NOsa Igiebor</option>
</SELECT>
<SELECT name='cand_name[]' class='form-control'>
<option></option>
<option>Ahmed MUSA</option>
<option>Ahmed Arinze</option>
<option>NOsa Igiebor</option>
</SELECT>
</html>

code below;

<?php
include_once 'dbcon.php';

{
$username = $_POST['username'];
    $cand_name = $_POST['cand_name'];
        $school = $_POST['school'];

            for ($i = 0; $i < count($username); $i++) {

        $username = ($username[$i]);
        $cand_name = ($cand_name[$i]);
        $school = ($school[$i]);

        mysqli_query($con, "INSERT INTO parlia (username, cand_name, school) VALUES ('$username', '$cand_name', '$school')");
    } 
}

i hav checked other SO answers but none seem to work for me. i have edited and it still doesn't work. when i tried this code with input, it worked perfectly but doesn't want to work with options

Onwu Bishop Gideon

this piece of code works for me.. you are however susceptible to sql injection

<?php
$username = $_POST['username'][0];
    $school = $_POST['school'][0];
        $candname = $_POST['candname'];
            foreach ($_POST['candname'] as $candname) {
     $query = sprintf(
            "INSERT INTO parlia_votes (username, school, candname) VALUES ('%s', '%s', '%s')",
            $username,
            $school,
            $candname
        );
        $con->query($query);
    }
?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Php While loop only displaying one record

How to select only one record of each category?

Modal window only pulling information from first record in a PHP for each loop

php Foreach Loop only extracting last record

(Validation) to Post only one record in a minute in MVC Post

PHP session records return only one record

foreach loop returns only one result of post

For loop in PHP with a unique output for only one loop

Displaying only one record from json object , using jquery each

How to retrieve a list of record including only one child each?

PHP: for each loop is displaying only last element

PHP for each Loop return only certain elements

PHP For each Loop returns one value

php for each loop sends one row to database

Wordpress Post Meta only working for one post in loop

foreach loop print only one record from MySQL table

Doctrine only inserts one record after foreach loop

mysqli search with while loop output only returns one record

Talend loop for each record

PHP only reading one $_POST value

php - foreach loop only runs one

How to get only one record from left table against each record from right table

Insert Only One Record In MYSQL database using php

javascript .push method inside .each loop only returning one array

how to edit only one post instead of iterative loop all posts?

Php - While [data] loop, $Post returning only first value

Rails has_many association how to guarantee only one of each associated record?

PowerShell: Two A records are shown for each SRV record - when there should only be one

In DQN, hwo to perform gradient descent when each record in experience buffer corresponds to only one action?