-1


Recently I got a problem with $_POST method. Here is select attribute in HTML that is giving informations by POST method.

<select name="date_year[]" required>
                    <option value="" selected="selected" disabled="true">Choose year...</option>
                    <option value="07"> 2007</option>
                    <option value="08"> 2008</option>
                    <option value="09"> 2009</option>
                    <option value="10"> 2010</option>
                    <option value="11"> 2011</option>
                    <option value="12"> 2012</option>
                    <option value="13"> 2013</option>
                    <option value="14"> 2014</option>
                    <option value="15"> 2015</option>
                    <option value="16"> 2016</option>
                    <option value="17"> 2017</option>
                    <option value="18"> 2018</option>
                    <option value="19"> 2019</option>
                    <option value="20"> 2020</option>
                    </select>

after submiting form with this select tag, in PHP code I have something like this:

$month_date = $_POST['date_month'];
$year_date = $_POST['date_year'];

$final_date = $month_date . ' '. $year_date;
$esult = $connection->query("SET NAMES 'utf8'");
if($connection->query("INSERT INTO thread VALUES (NULL, '$name', '$final_date', '$desc', '$thumbnail', '$gallery_img')")) {
    unset($_POST['upload']);
    header('Location: panel.php');
    $connection->close();
    exit();
}

Here just look for these $_POST things. I just gave the full code for the context. Here is my problem: after a successfull insert to my MySQL database, I got the value "Array Array". From curiosity I echo'ed that $final_date but still, it's just 'Array Array'. Why?

11
  • Have you used js to post that form? Commented Aug 17, 2017 at 12:38
  • because you defined your select as name="date_year[]", so it's an array. Remove the [] from teh name ro do you need it to be an array? Commented Aug 17, 2017 at 12:38
  • Remove [] from date_year[] from select Commented Aug 17, 2017 at 12:38
  • Hey y'all, few minutes after the question I have saw that. The problem was that "[]" in the name of my select attribute. Thanks for your time! Commented Aug 17, 2017 at 12:39
  • 1
    You are open to SQL injections. Parameterize the query. Commented Aug 17, 2017 at 12:42

5 Answers 5

2

Change

<select name="date_year[]" required>

Into

<select name="date_year" required>

The brackets make the input field an array hence the Array in your query.

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

Comments

2

There are two issue with your code:

<select name="date_year[]" required>

here there is no need to use name as an array. So change it to:

<select name="date_year" required>

and after concatenating:

$final_date = $month_date . ' '. $year_date;

the format is m-Y which is not acceptable for date column. The date column format is 'Y-m-d'

Comments

1

As you have mention date_year[] as array in select tag, It will be result as array

change your select tag to

<select name="date_year" required>

Comments

1

Your html select element was following

<select name="date_year[]" required>

So you should access it like this

$year_date = $_POST['date_year'][0];

Not Like

$year_date = $_POST['date_year'];

Comments

1

change this

<select name="date_year[]" required>

to this

<select name="date_year" required>

it will solve your problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.