复选框不会将值传递给DB

时间:2016-03-16 16:58:14

标签: php sql database wordpress checkbox

我使用php在我的网站上创建了一个联系表单,其中一些值,更具体地说是具有复选框类型输入的值,不会发送到我的数据库。我认为使用复选框或其他东西的方式肯定有问题。提交表格后,我会收到几个通知:

  

注意:未定义的索引:熨烫   第79行/Applications/XAMPP/xamppfiles/htdocs/wordpress/demo.php

     

注意:未定义的变量:POST in   第94行/Applications/XAMPP/xamppfiles/htdocs/wordpress/demo.php

其余代码似乎工作正常,数据已加载到我的数据库中。

以下是我认为错误的demo-form.php文件中的一些代码:

<h4>i) Cleaningservice:</h4>
     <p>Experience (in years): <input type="number" name="cleanExpInYears"/> </p>
     <p>Skills: <br>
       <input type="checkbox" name="basic" value="yes"> Basic cleaning<br>
       <input type="checkbox" name="kitchen" value="yes"> Kitchen tools<br>
       <input type="checkbox" name="laundry" value="yes"> Laundry<br>
       <input type="checkbox" name="ironing" value="yes"> Ironing<br>
       <input type="checkbox" name="window" value="yes"> Windows<br>
     <p> Please tell us about other skills: <input type="text" name="other_clean"/> </p>
     </p>

这是相应的demo.php文件,它连接到数据库并在那里存储数据:

/* Cleaningservice data is gathered... */

$clean_value1 = $_POST['cleanExpInYears'];
$clean_value2 = $_POST['basic'];
$clean_value3 = $_POST['kitchen'];
$clean_value4 = $_POST['laundry'];
$clean_value5 = $_POST['ironing'];
$clean_value6 = $_POST['window'];
$clean_value7 = $_POST['other_clean'];

/* ...and inserted into the table cleaningservice */

$sql = "INSERT INTO cleaningservice (applicant_id, cleanExpInYears, basic, kitchen, laundry, ironing, window, other)
        VALUES (LAST_INSERT_ID(),'$clean_value1', '$clean_value2', '$clean_value3', '$clean_value4', '$clean_value5', '$clean_value6', '$clean_value7')";

if (!mysql_query($sql)) {
 die('Error: ' . mysql_error());
}

我在这里想念的是什么? 提前致谢

2 个答案:

答案 0 :(得分:1)

如果实际选中了复选框,浏览器只会将复选框传递给PHP脚本。

因此,在PHP中,您必须在使用其内容之前检查$ _POST变量是否实际存在,并提供UNCHECKED即默认选项(如果它不存在)。

/* Cleaningservice data is gathered... */

$clean_value1 = $_POST['cleanExpInYears']);
$clean_value2 = isset($_POST['basic']) ? $_POST['basic'] : 'No';
$clean_value3 = isset($_POST['kitchen']) ? $_POST['kitchen'] : 'No';
$clean_value4 = isset($_POST['laundry']) ? $_POST['laundry'] : 'No';
$clean_value5 = isset($_POST['ironing']) ? $_POST['ironing'] : 'No';
$clean_value6 = isset($_POST['window']) ? $_POST['window'] : 'No';
$clean_value7 = $_POST['other_clean'];

为了避免错误消息,例如`数组索引不存在,您应该检查所有POST / GET值是否仍然存在。

/* Cleaningservice data is gathered... */

$clean_value1 = isset($_POST['cleanExpInYears']) ? $_POST['cleanExpInYears'] : '';

$clean_value2 = isset($_POST['basic']) ? $_POST['basic'] : 'No';
$clean_value3 = isset($_POST['kitchen']) ? $_POST['kitchen'] : 'No';
$clean_value4 = isset($_POST['laundry']) ? $_POST['laundry'] : 'No';
$clean_value5 = isset($_POST['ironing']) ? $_POST['ironing'] : 'No';
$clean_value6 = isset($_POST['window']) ? $_POST['window'] : 'No';

$clean_value7 = isset($_POST['other_clean']) ? $_POST['other_clean'] : '';

答案 1 :(得分:0)

您必须在名称和值之间切换

     <p>Skills: <br>
       <input type="checkbox" value="basic" name="yes"> Basic cleaning<br>
       <input type="checkbox" value="kitchen" name="yes"> Kitchen tools<br>
       <input type="checkbox" value="laundry" name="yes"> Laundry<br>
       <input type="checkbox" value="ironing" name="yes"> Ironing<br>
       <input type="checkbox" value="window" name="yes"> Windows<br>
     <p> Please tell us about other skills: <input type="text" name="other_clean"/> </p>
     </p>

在PHP方面,您会在var_dump($_POST['yes'])

中找到所选的值