0

This is my code on the first page:

<form method="post" class="pure-form pure-form-stacked" action="insert.php">
    <fieldset>
        <label for="select">Select Apartment:</label>
        <select name="selectedOption">
            <option name="apart[]" value="1"> 001  </option>
            <option name="apart[]" value="2"> 002  </option>
            <option name="apart[]" value="3"> 003  </option>
            <option name="apart[]" value="4"> 004  </option>
        </select>
        <br>
        <button type="submit" class="pure-button pure-button-primary">Submit</button>
    </fieldset>
</form>

on insert.php I have this:

$apart = $_POST['apart'];

if(empty($apart)) 
{
    echo("You didn't select any apartment!.");
} 
else{
    $N = count($apart);
    for($i=0; $i < $N; $i++)
    {
        $apartment = $apart[$i];
    }
}
mysql_query("INSERT INTO `reserves`(idApart) values('$apartment')");

Arent it correct? because I am getting an error and the value that the idApart gets is 0. Please help me.

BobCreativeDu
  • 39
  • 2
  • 10
  • 1
    Hint: A ` – Bad Wolf Jun 13 '14 at 13:31
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jun 13 '14 at 13:31
  • @Quentin oh didnt knew that thank you I will be more carefull now. – BobCreativeDu Jun 13 '14 at 13:43

1 Answers1

5

change

$apart = $_POST['apart'];

to

$apart = $_POST['selectedOption'];

also select option no need to name name="apart[]" if you want make select as an array change <select name="selectedOption[]"> but there is no need cause select returns only one value. for multiple values you need to multiple attribute in select

Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44