dsa99349
2016-04-14 07:11
浏览 81
已采纳

无法从HTML表单复选框中获取值以插入MySQL数据库列

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!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douqianbiao4216 2016-04-14 07:29
    已采纳

    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';
    
    打赏 评论
  • duanboniao5903 2016-04-14 07:22

    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')";
    
    打赏 评论
  • dongyihao9887 2016-04-14 08:49

    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

    打赏 评论

相关推荐 更多相似问题