0

I am new to this website and just want to say thank you in advance for any assistance. So, I have a form that's linked to a separate PHP script, and everything else on the form inserts into the database just fine except for the check boxes. The current code I have below received the "Undefined index:" error and I have tried following many tutorials but this is as far as I've gotten thus far.

Here is my HTML code for the checkboxes:

<input type="checkbox" name="problem_list[]" value="Handle Bars">Handle Bars<br>
        <input type="checkbox" name="problem_list[]" value="Frame">Frame<br>
        <input type="checkbox" name="problem_list[]" value="Pedals">Pedals<br>
        <input type="checkbox" name="problem_list[]" value="Chain">Chain<br>
        <input type="checkbox" name="problem_list[]" value="Brakes">Brakes<br>
        <input type="checkbox" name="problem_list[]" value="Gears">Gears<br>
        <input type="checkbox" name="problem_list[]" value="Tires">Tires<br>

And here is my PHP code for related to the checkboxes:

$problem_list = ""; // declare problem_list
if (isset($_POST["problem_list"])) 
{$problem_list = implode(" ,",$_POST['problem_list']);
}
$sql = "INSERT INTO bike_repairs (employee_id, date, first_name, last_name, phone, email, make, model, color, overall_problem_area, specific_issue) 
VALUES ('$value16', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12', '".$problem_list."', '$value14')";

Just to recap, the form works, the database connection is established, all other form elements are accounted for in variables and inserting just fine. So this is the last thing I need to figure out. Thanks again for any assistance!

Fahmi B.
  • 758
  • 2
  • 10
  • 23
  • Whats the datatype of your overall_problem_area field? – Indrajit Apr 14 '16 at 07:18
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Apr 14 '16 at 08:34
  • Don't put CSV data into database columns, you have a relational database, [use its relations](http://stackoverflow.com/questions/7296846/how-to-implement-one-to-one-one-to-many-and-many-to-many-relationships-while-de). – Quentin Apr 14 '16 at 08:35

3 Answers3

0

Declare $problem_list before if condition

 $problem_list="";
  if (isset($_POST["problem_list"])) 
    {
       $problem_list = implode(" ,",$_POST['problem_list']);
    }
    $sql = "INSERT INTO bike_repairs (employee_id, date, first_name, last_name, phone, email, make, model, color, overall_problem_area, specific_issue) 
    VALUES ('$value16', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12', '".$problem_list."', '$value14')";
Naisa purushotham
  • 905
  • 10
  • 18
0

if you edit like this, what you got ?

if ( !empty($_POST["problem_list"]) ) 
{
    $problem_list = implode(" ,",$_POST['problem_list']);
    $sql = "INSERT INTO bike_repairs (employee_id, date, first_name, last_name, phone, email, make, model, color, overall_problem_area, specific_issue) 
    VALUES ('$value16', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12', '".$problem_list."', '$value14')";
}
else echo 'problem_list is empty';
Fahmi B.
  • 758
  • 2
  • 10
  • 23
0

You declared problem_list chackboxes as array (name='problem_list[]'). So, just check if it is an array and implode it.

$problem_list = $_POST["problem_list"];
if(is_array($problem_list)) {
$selected_problems = implode(" ,",$problem_list);
} else {
$selected_problems = "No problems!";
}

$problem_list = implode(" ,",$_POST['problem_list']);

$problem_list = mysql_real_escape_string($problem_list , $connection); // IMPORTANT

$sql = "INSERT INTO bike_repairs (employee_id, date, first_name, last_name, phone, email, make, model, color, overall_problem_area, specific_issue) 
VALUES ('$value16', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12', '".$problem_list."', '$value14')";

Never forget to escape string values before execute a query to prevent sql injection attack.

Take a look at:

http://php.net/manual/pt_BR/function.mysql-real-escape-string.php

Tiago Luz
  • 129
  • 6
  • Thank you for the input, will take a look at that and update code accordingly. –  Apr 15 '16 at 15:24