2016-06-07 2 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와 최신 아파치와 MySQL. 누군가가 잘못되었거나 내가 잘못하고있는 것을 말해 줄 수 있습니까?

+0

PHP가 실행되지 않고 수십 개의 구문 오류가 있습니다. – Barmar

답변

4

여기를 참조하십시오.>$_POST'firstname'; 및 다른 모든 POST 어레이?

괄호가 누락되었습니다 ([]).

$_POST['firstname'];으로 변경하고 다른 모든 경우도 동일하게 변경하십시오.

양식에 대한 설명서를 읽는 것처럼 오류보고가 도움이 될 것입니다.

  • http://php.net/manual/en/tutorial.forms.php
  • 플러스 http://php.net/manual/en/function.error-reporting.php

    • , 당신은 당신의 데이터베이스에가는 모든 POST 배열에 대한 조건부 !empty()를 사용해야합니다.

      데이터베이스가 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 또는 PDOprepared statements을 사용하십시오.

    +0

    구문 오류가있을 때 오류보고는 스크립트에서 아무 것도 실행되지 않으므로 도움이되지 않습니다. – Barmar

    +0

    @Barmar 내가 놓친 게 있니? –

    +0

    @Barmar 좋아, 이제 더 이상 발견하지 못했습니다. 로드니, 그 전체 줄을 다시 써야 할 것 같아. –

    관련 문제