2015-08-25 66 views
1

我在StackOverflow上发现了一些主题,并且读了一些关于如何将复选框中的数据插入MySQL数据库的内容。我读了关于使用数组将值插入数据库。现在的问题是,只要按下提交按钮(除了复选框值),我的表单中的所有数据都会包含在内。所以表单就像它应该的那样工作,只有复选框值不会被插入到我的数据库中。下面是我做的:PHP/MySQL - 数据库中的复选框

我的表单中的一部分:

<form method="post" action="add_schulen.php" class="form-horizontal form-label-left"> 

<div class="form-group"> 
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Name Schule: </label> 
<div class="col-md-9 col-sm-9 col-xs-12"> 
    <input type="text" name="schulname" class="form-control" placeholder="Namen der Schule"> 
</div> 
</div> 

    <div class="col-md-3 col-sm-3 col-xs-12 profile_left"> 
     <input type="checkbox" name="schulen[]" value="1A"> 1A <br /> 
     <input type="checkbox" name="schulen[]" value="1B"> 1B <br /> 
     <input type="checkbox" name="schulen[]" value="1C"> 1C <br /> 
     <input type="checkbox" name="schulen[]" value="1D"> 1D <br /> 
     <input type="checkbox" name="schulen[]" value="1E"> 1E <br /> 
     <input type="checkbox" name="schulen[]" value="1F"> 1F <br /> 
     <input type="checkbox" name="schulen[]" value="1G"> 1G <br /> 
     <input type="checkbox" name="schulen[]" value="1H"> 1H <br /> 
    </div> 
<div class="col-md-3 col-sm-3 col-xs-12 profile_left"> 

     <input type="checkbox" name="schulen[]" value="2A"> 2A <br /> 
     <input type="checkbox" name="schulen[]" value="2B"> 2B <br /> 
     <input type="checkbox" name="schulen[]" value="2C"> 2C <br /> 
     <input type="checkbox" name="schulen[]" value="2D"> 2D <br /> 
     <input type="checkbox" name="schulen[]" value="2E"> 2E <br /> 
     <input type="checkbox" name="schulen[]" value="2F"> 2F <br /> 
     <input type="checkbox" name="schulen[]" value="2G"> 2G <br /> 
     <input type="checkbox" name="schulen[]" value="2H"> 2H <br /> 
</div> 
    <button type="submit" name="addschulen" value="addschulen" class="btn btn-warning btn-lg pull-right">Schule anlegen</button> 
</form> 

我add_schulen.php顶我的完整的PHP代码:

<?php 
include 'inc/database.php';  

// Check if form is submitted 
if (isset ($_POST['addschulen'])) { 
    $schulname = mysqli_real_escape_string ($connect, $_POST['schulname']); 
    $ansprechperson = mysqli_real_escape_string ($connect, $_POST['ansprechperson']); 
    $schulstrasse = mysqli_real_escape_string ($connect, $_POST['schulstrasse']); 
    $schulplz = mysqli_real_escape_string ($connect, $_POST['schulplz']); 
    $schulort = mysqli_real_escape_string ($connect, $_POST['schulort']); 
    $schultelefon = mysqli_real_escape_string ($connect, $_POST['schultelefon']); 
    $klassen = mysqli_real_escape_string ($connect, $_POST['schulen']); 

     // Setting up a blank variable to be used in the coming loop. 
     $alleKlassen = ""; 

     // For every checkbox value sent to the form. 
     foreach ($klassen as $klasse) { 
     // Append the string with the current array element, and then add a comma and a space at the end. 
     $alleKlassen .= $klasse . ", "; 
     } 

     $query_insert_schule = mysqli_query($connect,"INSERT INTO `schule` (`schulname`, `ansprechperson`, `schulstrasse`, `schulplz`, `schulort`, `schultelefon`, `klasse`) 
      VALUES ('$schulname', '$ansprechperson', '$schulstrasse', '$schulplz', '$schulort', '$schultelefon', '$alleKlassen')"); 

if (mysqli_affected_rows($connect) == 0) //<-- 
{ 
    die('Could not update data: ' . mysql_error()); 
} else { 
    $msg_success= '<strong>Gratulation!</strong> Die Schule wurde erfolgreich hinzugefügt. Zur <a href="schulverwaltung.php"><span style="color:#fff;">Übersicht aller Schulen >></span></a>'; 
}  

}   
?> 

正如我所说的所有的数据都包括只要我点击提交按钮,除了复选框的值。有没有人有一个想法,为什么?我做错了什么?这是我第一次尝试将复选框值插入到我的数据库中。

谢谢, 克里斯

+0

尝试'$克拉森= mysqli_real_escape_string($连接时,$ _ POST [ 'schulen []']);' – RamRaider

+0

谢谢你的帮助,但仍是同样的问题。一切进入我的数据库,除了复选框的值 –

+0

什么'var_dump($ klassen);'给(在任何修改之前)? – Qirel

回答

1

以及我试了一下,它只是没有插入数据库,但我只是打印出来。尝试从$_POST['schulen']中删除字符串转义,因为它已经清除/预定义数据。并使用substr()删除数组末尾的最后一个逗号。

试试这个:

<?php 
// Check if form is submitted 
if (isset ($_POST['addschulen'])) { 
    $schulname = $_POST['schulname']; 
    $ansprechperson =$_POST['ansprechperson']; 
    $schulstrasse = $_POST['schulstrasse']; 
    $schulplz =$_POST['schulplz']; 
    $schulort = $_POST['schulort']; 
    $schultelefon =$_POST['schultelefon']; 
    $klassen = $_POST['schulen']; 

     // Setting up a blank variable to be used in the coming loop. 
     $alleKlassen = ""; 

     // For every checkbox value sent to the form. 
     foreach ($klassen as $klasse) { 
     // Append the string with the current array element, and then add a comma and a space at the end. 
     $alleKlassen .= $klasse . ", "; 
     } 
    $alleKlassen = substr($alleKlassen , 0, -2); 
    print_r($alleKlassen); 

}else{ 
    echo '<form method="post" action="" class="form-horizontal form-label-left"> 

<div class="form-group"> 
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Name Schule: </label> 
<div class="col-md-9 col-sm-9 col-xs-12"> 
    <input type="text" name="schulname" class="form-control" placeholder="Namen der Schule"> 
</div> 
</div> 

    <div class="col-md-3 col-sm-3 col-xs-12 profile_left"> 
     <input type="checkbox" name="schulen[]" value="1A"> 1A <br /> 
     <input type="checkbox" name="schulen[]" value="1B"> 1B <br /> 
     <input type="checkbox" name="schulen[]" value="1C"> 1C <br /> 
     <input type="checkbox" name="schulen[]" value="1D"> 1D <br /> 
     <input type="checkbox" name="schulen[]" value="1E"> 1E <br /> 
     <input type="checkbox" name="schulen[]" value="1F"> 1F <br /> 
     <input type="checkbox" name="schulen[]" value="1G"> 1G <br /> 
     <input type="checkbox" name="schulen[]" value="1H"> 1H <br /> 
    </div> 
<div class="col-md-3 col-sm-3 col-xs-12 profile_left"> 

     <input type="checkbox" name="schulen[]" value="2A"> 2A <br /> 
     <input type="checkbox" name="schulen[]" value="2B"> 2B <br /> 
     <input type="checkbox" name="schulen[]" value="2C"> 2C <br /> 
     <input type="checkbox" name="schulen[]" value="2D"> 2D <br /> 
     <input type="checkbox" name="schulen[]" value="2E"> 2E <br /> 
     <input type="checkbox" name="schulen[]" value="2F"> 2F <br /> 
     <input type="checkbox" name="schulen[]" value="2G"> 2G <br /> 
     <input type="checkbox" name="schulen[]" value="2H"> 2H <br /> 
</div> 
    <button type="submit" name="addschulen" value="addschulen" class="btn btn-warning btn-lg pull-right">Schule anlegen</button> 
</form>'; 
} 
?> 
+0

非常感谢你!而已!这是因为字符串逃脱!我不知道 :)!还有最后一件事:当我现在想要将这些复选框从数据库输出到下拉列表中时,您还可以告诉我我的代码应该是什么样子?因此,在另一个页面上,用户可以从下拉列表中选择每个已包含在我的数据库中的复选框? –

+0

ohkk我希望这可以帮助,当你有一个像php这样的字符串'$ checkbox =“1A,2C,7N”'''你可以使用['explode()'](http://php.net/manual /en/function.explode.php)函数来单独获取它们,以便将其放入下拉菜单中。试试这个或看看[这里](http://codepad.org/t0z1W3Vg) – Mystro

+0

谢谢!我看了一下键盘链接,我会试试看。再一次感谢你! –

0

由于您使用的HTML数组必须foreach所他们像数组:

$schulen= $_POST['schulen']; 
foreach($schulen as $s){ 
    //code 
} 
现在

,如果你写:回声$ S; (下//代码),将打印用户进行了检查,该复选框的值...所以现在应该很容易的为您解决;) 例如,如果你想逃避:

$schulen= $_POST['schulen']; 
for($i=0; $i< count($schulen); $i++){ 
    $schulen[ $i ]= mysqli_real_escape_string($schulen[ $i ]); 
} 

如果你想放入数据库你需要一个合适的提交(例如:一个文本字段将是好的) 然后你必须使用implode &爆炸... 把数组:$ todatabase = implode($ schulen,'| '); 获取数组:$ schulen = explode('|',$ fromdatabase);

+0

我现在写了:foreach($ klassen as $ klasse){echo $ klasse; }一旦我提交我的表单,它是一样的...一切都插入我的数据库,除了复选框的值 –

+0

阅读我的编辑,使用内爆和爆炸;) –

+0

他不必使用'implode'或'explode',他正在从'foreach'的数组中构建一个字符串。 – Qirel