PHP - 数据库中缺少的条目(bind_param)

时间:2018-02-19 11:12:54

标签: php

早上好, 我尝试填充一个数据库(使用PHP和HTML),并遵循目前为止的教程。 在几个成功的章节之后,我被困在Point,我必须用一些数据填充数据库 一切似乎都很好。但是当我保存条目时,它只存储3个值。 在我的例子中,这是列" id" " created_at"和"文字"。 其他列保持空白。 我使用:

  • PHP Version 7.1.14
  • Apache WebServer 2.0(在Windows上)
  • MSVC14(Visual C ++ 2015)

(德语)Tutorial



<?php
require ('dbconnect.php');
$db = mysqli_connect (MYSQL_HOST, MYSQL_BENUTZER, MYSQL_KENNWORT, MYSQL_DATENBANK);
if ($db->connect_errno) {
    die('Sorry');
}				
$db->set_charset('utf8');
if (isset($_POST['aktion']) and ($_POST['aktion']=='speichern')) {
    $vorname = "";
    if (!isset($_POST['vorname'])) {
        $vorname = trim($_POST['vorname']);	
    }
	$email = "";
    $if (!isset($_POST['email'])) {
        $email = trim($_POST['email']);
    }
    $nachname = "";
    if (isset($_POST['name'])) {
        $name = trim($_POST['name']);
    }
    $text = "";
    if (isset($_POST['text'])) {
        $text = trim($_POST['text']);
    }
	$funktion="";
	
    $created_at = date("Y-m-d H:i:s"); 
    
	if ( $vorname != '' or $name != '' or $created_at != '' or $text != '')
    {
     $einfuegen = $db->prepare("INSERT INTO gaestebuch (name, email, text, vorname, funktion, created_at) VALUES (?,?,?,?,?,NOW())");
	 $einfuegen->bind_param('sssss',  $vorname, $email, $text, $nachname,  $funktion);
	 $einfuegen->execute();

	if ($einfuegen->execute()) {
            header('Location: config.php?aktion=feedbackgespeichert');
            die();
            echo "<h1>gespeichert</h1>";
        }
    }	
}
if (isset($_POST['aktion']) and $_POST['aktion']=='feedbackgespeichert') {
    echo "<p>Es liegen keine Daten vor :(</p>";
}
$daten = array();
  if ($erg = $db->query("SELECT * FROM gaestebuch")){
  if ($erg->num_rows) {
	 while($datensatz = $erg->fetch_object()) {
            $daten[] = $datensatz;
        }
		$erg->free();
    }	
} 
if (!count($daten)) {
    echo "<p>no data :</p>";
}
else {
?>
 <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>name</th>
		    <th>vorname</th>
                <th>email</th>
                <th>text</th>
		    <th>funktion</th>
                <th>created_at</th>
		    <th>deleted_at</th>
				
            </tr>
        </thead>
        <tbody>
			<?php
			foreach ($daten as $inhalt){
		?>
            <tr>
                    <td><?php echo $inhalt->id; ?></td>
                    <td><?php echo $inhalt->name; ?></td>
                    <td><?php echo $inhalt->vorname; ?></td>
                    <td><?php echo $inhalt->email; ?></td>
                    <td><?php echo $inhalt->text; ?></td>
			<td><?php echo $inhalt->funktion; ?></td>
		        <td><?php echo $inhalt->created_at; ?></td>
                    <td><?php echo $inhalt->deleted_at; ?></td>
            </tr>
			<?php
			}
			?>
        </tbody>
    </table>
<?php	
}
?>	
<form action="" method="post">
    <label>Vorname: 
	<input type="text" name="vorname" id="vorname">
    </label>
	
    <label>Nachname: 
        <input type="text" name="name" id="name">
    </label>
	
	 <label>email: 
        <input type="text" name="email" id="email">
    </label>
	
    <label>text: 
        <textarea name="text" id="text"></textarea>
    </label>
	
	 <label>funktion: 
         <textarea name="funktion" id="funktion"></textarea>
    </label>
			<input type="hidden" name="aktion" value="speichern">
			<input type="submit" value="speichern">
</form>
&#13;
&#13;
&#13;

任何人都可以帮助我吗? :)

1 个答案:

答案 0 :(得分:0)

更新

你的2如果条件在逻辑上是错误的:

这些

if (!isset($_POST['vorname'])) {
    $vorname = trim($_POST['vorname']); 
}
$email = "";
$if (!isset($_POST['email'])) {
    $email = trim($_POST['email']);
}

应该是:

if (isset($_POST['vorname'])) {
    $vorname = trim($_POST['vorname']); 
}
$email = "";
$if (isset($_POST['email'])) {
    $email = trim($_POST['email']);
}