1

I have a following list of checkboxes:

<input type="checkbox" name="day_of_week" value="1">Monday
<input type="checkbox" name="day_of_week" value="2">Tuesday
<input type="checkbox" name="day_of_week" value="3">Wednessday
<input type="checkbox" name="day_of_week" value="4">Thursday
<input type="checkbox" name="day_of_week" value="5">Friday
<input type="checkbox" name="day_of_week" value="6">Saturday
<input type="checkbox" name="day_of_week" value="7">Sunday

After user submits the full form, I receive it in another file:

$week_days = mysqli_real_escape_string($this->db->conn_id, $_POST['day_of_week'])

But then $week_days only contains the value of the last checked checkbox, not all of them. How can I receive all the values?

Suiko
  • 17
  • 5
  • Possible duplicate of [Multiple inputs with same name through POST in php](https://stackoverflow.com/questions/7880619/multiple-inputs-with-same-name-through-post-in-php) – Mayank Vadiya Aug 18 '17 at 13:58

1 Answers1

3

The name should be an array.

<input type="checkbox" name="day_of_week[]" value="1">Monday
<input type="checkbox" name="day_of_week[]" value="2">Tuesday
<input type="checkbox" name="day_of_week[]" value="3">Wednessday
<input type="checkbox" name="day_of_week[]" value="4">Thursday
<input type="checkbox" name="day_of_week[]" value="5">Friday
<input type="checkbox" name="day_of_week[]" value="6">Saturday
<input type="checkbox" name="day_of_week[]" value="7">Sunday

Hope this helps.


For your second error, mysqli_real_escape_string accepts second parameter as string and you are passing array. Please check this

string mysqli_real_escape_string ( mysqli $link , string $escapestr )

Please use for loop to solve that error.

Nikhil G
  • 2,096
  • 14
  • 18
  • Produces a following error: Message: mysqli_real_escape_string() expects parameter 2 to be string, array given – Suiko Aug 18 '17 at 14:00
  • Solved it like this: `$week_days = '';` `for($i=0;$i<7;++$i){ $week_days .= (isset($_POST['day_of_week'][$i])) ? mysqli_real_escape_string($this->db->conn_id,$_POST['day_of_week'][$i]) : '';}` – Suiko Aug 18 '17 at 14:16
  • Yes, that's what I said. You need to use loop to solve it. – Nikhil G Aug 18 '17 at 14:18