【问题标题】:using html post function with checkboxes [duplicate]使用带有复选框的html发布功能[重复]
【发布时间】:2017-06-11 23:16:26
【问题描述】:

我正在尝试使用带有 php 的 html post 函数将 1 或 0 插入到数据库中,具体取决于用户是否勾选了复选框,但是每当我提交表单时,我都会收到索引错误,无论是否复选框是否选中。我该如何防止这种情况发生?我得到的错误是“注意:未定义的索引:第 15 行 C:\xampp\htdocs\scout\insertbadgeinphp.php 中的冒险挑战奖”

<?php
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "scout";


$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
if (isset($_POST['submit'])) {

if (!$_POST['Adventure Challenge Award'] == null)
{
    $aca = "1";
}
else{
    $aca = "0";
}

}


$sql = "INSERT INTO insertbadgescout (AdventureChallengeBadge)
VALUES ('$aca');";


if ($conn->multi_query($sql) === TRUE) {

    echo "New records inserted successfully");

}
else 
{
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$conn->close();
?>




<!DOCTYPE html>
<html> 
<head> 
<title>Insert badges scouts have</title> 

</head> 
<body id="body-color"> 
<div id="badges"> 


<form method="POST" action="insertbadgeinphp.php"> 

<input type="checkbox" name="Adventure Challenge Award" value="yes">Adventure Challenge Award<br>

<input id="button" type="submit" name="submit" value="Save Badges"> 
</form> 

</div> 
</body>
<p><a href="Homepage.html"> Home </a> </p> 
</html> 

【问题讨论】:

  • 你能告诉我们你的表单代码吗?
  • 谢谢,如下所述,不要在名称属性中使用空格。

标签: php html mysql phpmyadmin xampp


【解决方案1】:

请在您的代码中进行以下修改并尝试

<input type="checkbox" name="Adventure_Challenge_Award" value="yes">Adventure Challenge Award<br>

在您的 PHP 帖子部分中

if ($_POST['Adventure_Challenge_Award'] == 'Yes')
{
  $aca = "1";
}
else
{
$aca = "0";
}

【讨论】:

    【解决方案2】:

    您的 $_POST['Adventure Challenge Award'] 未定义,这不是从表单发送的,因为它使用空格。

    小贴士:

    • 不要在变量中使用空格(就像你一样)
    • 使用可以快速阅读和易于理解的简单名称,例如(agreementRadio act.)
    • 总是,我的意思是总是转义你传递给查询的字符串...More information here
    • 将数据发送到另一个文件,例如 Registration.php。

    【讨论】:

      【解决方案3】:

      请勿在您的姓名中使用空格="my_input"。请改用 undescore。 你可以改变:

      if (!$_POST['Adventure Challenge Award'] == null)
      

      if (!isset($_POST['Adventure Challenge Award']))
      

      检查变量是否存在

      【讨论】:

        【解决方案4】:

        试试这个,不要在 $_POST['Adventure Challenge Award'] 中使用空格

        $servername = "127.0.0.1";
        $username = "root";
        $password = "";
        $dbname = "scout";
        
        
        $conn = new mysqli($servername, $username, $password, $dbname);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 
        if (isset($_POST['submit'])) {
        
        if (!$_POST['Adventure_Challenge_Award'] == null)
        {
            $aca = "1";
        }
        else{
            $aca = "0";
        }
        
        }
        
        
        $sql = "INSERT INTO insertbadgescout (`AdventureChallengeBadge`)
        VALUES ('$aca');";
        
        if ($conn->query($sql)) {
            echo "New records inserted successfully");
        } else {
            echo "Error";
        }
        
        
        $conn->close();
        ?>
        

        【讨论】:

          猜你喜欢
          • 2023-03-07
          • 2011-06-20
          • 1970-01-01
          • 1970-01-01
          • 2010-10-17
          • 1970-01-01
          • 2013-05-21
          • 2017-02-14
          • 1970-01-01
          相关资源
          最近更新 更多