Данных не найдено в моей базе данных при запуске кода ниже.
Я продолжаю получать ошибку «Не могу написать в базу данных».
Here - изображение моей базы данных, с данными, введенными вручную через phpmyadmin.
Here - это еще одно изображение с моей стеной стола.
HTML форма
<form method="post" action="report.php">
<label for="firstname">First name:</label>
<input type="text" id="firstname" name="firstname" placeholder="test"/><br />
<label for="lastname">Last name:</label>
<input type="text" id="lastname" name="lastname" /><br />
<label for="email">What is your email address?</label>
<input type="text" id="email" name="email" /><br />
<label for="whenithappened">When did it happen?</label>
<input type="text" id="whenithappened" name="whenithappened" /><br />
<label for="howlong">How long were you gone?</label>
<input type="text" id="howlong" name="howlong" /><br />
<label for="howmany">How many did you see?</label>
<input type="text" id="howmany" name="howmany" /><br />
<label for="aliendescription">Describe them:</label>
<input type="text" id="aliendescription" name="aliendescription" size="32" /><br />
<label for="whattheydid">What did they do to you?</label>
<input type="text" id="whattheydid" name="whattheydid" size="32" /><br />
<label for="fangspotted">Have you seen my dog Fang?</label>
Yes <input id="fangspotted" name="fangspotted" type="radio" value="yes" />
No <input id="fangspotted" name="fangspotted" type="radio" value="no" /><br />
<img src="fang.jpg" width="100" height="175"
alt="My abducted dog Fang." /><br />
<label for="other">Anything else you want to add?</label>
<textarea id="other" name="other"></textarea><br />
<input type="submit" value="Report Abduction" name="submit" />
</form>
</body>
report.php
<?php
$name = $_POST['firstname'] . ' ' . $_POST['lastname'];
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$when_it_happened = $_POST['whenithappened'];
$how_long = $_POST['howlong'];
$how_many = $_POST['howmany'];
$what_they_did = $_POST['whattheydid'];
$alien_description = $_POST['aliendescription'];
$fang_spotted = $_POST['fangspotted'];
$other = $_POST['other'];
$email = $_POST['email'];
$to = '[email protected]';
$subject = 'Aliens abducted med - Abduction report';
$msg = "$name was abducted $when_it_happened and was gone for $how_long \n" .
"Number of aliens: $how_many\n" .
"Alien description: $alien_description\n" .
"What they did: $what_they_did\n" .
"Fang spotted: $fang_spotted\n" .
"Other comments: $other";
$dbc = mysqli_connect('localhost', 'root', '', 'phpheadfirst')
or die ('Error. Can\'t connect to the databse');
$query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
"how_many, alien_description, what_they_did, fang_spotted, other, email) " .
"VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
"'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";
$result = mysqli_query($dbc, $query)
or die('Error. Can\'t write to the database');
mysqli_close($dbc);
echo 'Hello ' . $name . ', and thanks for submitting the form.' . '<br />';
echo 'You were abducted ' . $when_it_happened;
echo ' by ' . $how_many . ' aliens';
echo ' and were gone for ' . $how_long . '<br />';
echo 'What did they do to you? ' . $what_they_did . '<br />';
echo 'Describe them: ' . $alien_description . '<br />';
echo 'Was fang there? ' . $fang_spotted . '<br />';
echo 'Do you have more to add?' . $other . '<br />';
echo 'Your email adress is ' . $email;
mail($to, $subject, $msg, 'From:' . $email);
Я пытался найти проблему, но я не могу.
Я использую самую новую установку XAMPP, и все это работает.
Пробовал также код на моем онлайн-сервере - по-прежнему та же проблема.
Не поймайте ошибку, чтобы получить лучшее сообщение об ошибке. Комментарий: «или умереть» («Ошибка. Не могу писать в базу данных»); ' – dan08
@ dan08 Если я прокомментирую или удалю его, код будет запущен и покажет эхо, но все равно не будет вставляться в базу данных, и нет сообщения об ошибке. – Subvenio
Пришельцы будут похищать ваш сайт, если вы не добавите некоторую базовую безопасность в эту форму. –