2

i have a php for each loop that post only one record to database and it does not return any errors. i have checked my html and obviously there's nothin wrong it. i tried some SO options but still no results. here is my html

<form method="post" action="index.php">
 <input type="text" name="username[]" value="12345" readonly="readonly" />
 <input type="text" name="school[]" value="Degree" readonly="readonly" />
 <select name="candname[]">
    <option></option>
    <option>wayne roony</option>
    <option>ikpa oludo</option>
    <option>meta</option>
    <option>databoy</option>
    <option>lanre</option>
    <option>toafeek</option>
    <option>shola suni</option>
</select>
<br/>
<select name="candname[]">
    <option></option>
    <option>wayne roony</option>
    <option>ikpa oludo</option>
    <option>meta</option>
    <option>databoy</option>
    <option>lanre</option>
    <option>toafeek</option>
    <option>shola suni</option>
</select>
<br/>
<select name="candname[]">
    <option></option>
    <option>wayne roony</option>
    <option>ikpa oludo</option>
    <option>meta</option>
    <option>databoy</option>
    <option>lanre</option>
    <option>toafeek</option>
    <option>shola suni</option>
</select>
<br/>
<select name="candname[]">
    <option></option>
    <option>wayne roony</option>
    <option>ikpa oludo</option>
    <option>meta</option>
    <option>databoy</option>
    <option>lanre</option>
    <option>toafeek</option>
    <option>shola suni</option>
</select>
<br/>
<select name="candname[]">
    <option></option>
    <option>wayne roony</option>
    <option>ikpa oludo</option>
    <option>meta</option>
    <option>databoy</option>
    <option>lanre</option>
    <option>toafeek</option>
    <option>shola suni</option>
</select>

and my PHP

<?php 

$con=mysqli_connect("localhost","root","4***","online**");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

 {
    $username = $_POST['username'];
    $school = $_POST['school'];
    $candname = $_POST['candname'];
    for ($i = 0; $i < count($username); $i++) {

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

        mysqli_query($con, "INSERT INTO parlia_votes (username, school, candname) VALUES ('$username', '$school', '$candname')");
    } 
}
?>

my aim is to post all five selected options to the database. thanks for the help

3
  • According to your form have only one username field. Commented Jun 18, 2017 at 9:14
  • Loop of the five options, not over the one username ;) Commented Jun 18, 2017 at 9:14
  • your script is prone to SQL injection. Consider using parametrized queries. Commented Jun 18, 2017 at 9:20

1 Answer 1

2

You have multiple candname but username and school both are single. still you take array for all elements in form you cantry follwing code

    ...

    $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);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I simplified the code in the answer, however it misses proper query creation. The example in short shows an example of SQL injection via $_POST variables.
Yes you are right @hakre :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.