2016-06-07 3 views
1

Этот сервер подтверждает соединение с PHP, но значения, введенные в форму, не сохраняются только в пустых строках таблицы.PHP, отправляющий пустые значения формы на сервер

Код PHP:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Aliens Abducted Me - Report an Abduction</title> 
</head> 
<body> 
    <h2>Aliens Abducted Me - Report an Abduction</h2> 

<?php 


    $name = $_POST'firstname'; 
    $last_name =$_POST'lastname'; 
    $when_it_happened = $_POST'whenithappened'; 
    $how_long = $_POST'howlong'; 
    $how_many = $_POST'howmany'; 
    $alien_description = $_POST'aliendescription'; 
    $what_they_did = $_POST'whattheydid'; 
    $fang_spotted = $_POST'fangspotted'; 
    $email = $_POST'email'; 
    $other = $_POST'other'; 

    $dbc = mysqli_connect('localhost', 'root', 'Password', 'aliendatabase') 
    or die('Error Connecting to MySQL server'); 

    $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 Querying Database'); 

    mysqli_close($dbc); 

    echo 'Thanks for submitting the form.<br />'; 
    echo 'You were abducted ' . $when_it_happened; 
    echo ' and were gone for ' . $how_long . '<br />'; 
    echo 'Number of aliens: ' . $how_many . '<br />'; 
    echo 'Describe them: ' . $alien_description . '<br />'; 
    echo 'The aliens did this: ' . $what_they_did . '<br />'; 
    echo 'Was Fang there? ' . $fang_spotted . '<br />'; 
    echo 'Other comments: ' . $other . '<br />'; 
    echo 'Your email address is ' . $email; 

?> 

</body> 
</html> 

Вот форма/HTML код

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Aliens Abducted Me - Report an Abduction</title> 
    <link rel="stylesheet" type="text/css" href="style.css" /> 
</head> 
<body> 
    <h2>Aliens Abducted Me - Report an Abduction</h2> 

    <p>Share your story of alien abduction:</p> 
    <form method="post" action="report.php"> 
    <label for="firstname">First name:</label> 
    <input type="text" id="firstname" name="firstname" /><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> 
</html> 

код прямо из книги. php v5.6.22 и последние Apache и MySQL. Может ли кто-нибудь сказать мне, что может быть неправильно или что я делаю неправильно

+0

Ваш PHP не может быть запущен, он имеет десятки синтаксических ошибок. – Barmar

ответ

4

Посмотрите это =>$_POST'firstname'; и все остальные массивы POST?

У них отсутствуют скобки [] для них.

Так измените это на $_POST['firstname']; и сделайте то же самое для всех остальных.

Отчет об ошибках помог бы вам здесь, так же как прочитать руководство по формам.

Кроме того, вы должны использовать условную !empty() против всех массивов POST происходит в вашей базе данных.

Это может вызвать ошибки, если любой из них оставлен пустым, если ваша база данных не принимает значения NULL.

У вас также есть недостающая котировка для ('$first_name, и объединяет в вашем запросе.

Это необходимо переписать так:

$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')"; 

Сноски:

Ваш нынешний код открыт для SQL injection. Используйте prepared statements, или PDO с prepared statements.

+0

Отчеты об ошибках не помогают при наличии синтаксических ошибок, так как ничего в скрипте не будет запущено. – Barmar

+0

@ Barmar Я что-то пропустил? –

+0

@Barmar Хорошо, я заметил больше сейчас. Лорд, я думаю, мне придется переписать эту целую строчку. –

 Смежные вопросы

  • Нет связанных вопросов^_^