简体   繁体   English

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

[英]Having trouble getting the values from HTML Form check boxes to insert into a MySQL database column

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. 因此,我有一个链接到单独的PHP脚本的表单,该表单上的所有其他内容(除了复选框之外)都可以很好地插入数据库中。 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: 这是我的复选框的HTML代码:

<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: 这是我与复选框相关的PHP代码:

$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! 再次感谢您的协助!

Declare $problem_list before if condition 在if条件之前声明$ problem_list

 $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')";

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';

You declared problem_list chackboxes as array (name='problem_list[]'). 您将problem_list chackboxes声明为数组(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. 在执行查询以防止SQL注入攻击之前,请不要忘记转义字符串值。

Take a look at: 看一眼:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM