2016-07-29 71 views
0

一切工作只是罰款,直到我從

<form method='post' action='re.php'> 

改變了下面的代碼,以

<form method='post' action='<?php echo $_SERVER["PHP_SELF"];?>'> 

它給了我的HTTP錯誤500.我已經做了一些谷歌上搜索,但仍然不知道爲什麼會這樣!非常感謝您的幫助!

下面是整個腳本

<?php 

$hostname = 'localhost'; 
$username = 'Ciara'; 
$password = 'mypass'; 
$database = 'users';   

$conn = mysqli_connect($hostname,$username, $password, $database); 
if (!$conn) { 
    die ("Databas connect failed: " . mysqli_connect_error()); 
} else { 
    echo "Connected successfully. <br>"; 
} 

echo <<<_END 
<html> 
    <head> 
     <title>Register</title> 
    </head> 
    <body><center> 
     <form method='post' action='<?php echo $_SERVER["PHP_SELF"];?>'> 
     Firstname: <input type='text' name='firstname'><br> 
     Lastname: <input type='text' name='lastname'><br> 
     E-mail: <input type='text' name='email'><br> 
     Gender: <input type='radio' value='female' name='gender'>Female 
       <input type='radio' value='male' name='gender'>Male<br> 
     Username: <input type='text' name='username'><br> 
     Password: <input type='text' name='password'><br> 
     <input type='submit' value='Submit Form'> 
     </form></center> 

_END; 

if (isset($_POST['firstname']) && 
    isset($_POST['lastname']) && 
    isset($_POST['email']) && 
    isset($_POST['gender']) && 
    isset($_POST['username']) && 
    isset($_POST['password'])) { 

    $firstname = get($conn, 'firstname'); 
    $lastname = get($conn, 'lastname'); 
    $email = get($conn, 'email'); 
    $gender = get($conn, 'gender'); 
    $username = get($conn, 'username'); 
    $password = get($conn, 'password'); 

    $query = "insert into useracc values" . "(null, '$firstname','$lastname','$email','$gender','$username','$password')"; 

    if (mysqli_query($conn, $query)) { 
     echo "You have successfully registered! <br>"; 
    } else { 
     echo "Failed to register." . mysqli_error($conn); 
    } 
} 

mysqli_close($conn); 
echo "</body></html>"; 

function get($conn, $var) { 
    return mysqli_real_escape_string($conn, $_POST[$var]); 
} 
?> 

回答

0

,當你使用echo

<?php 

$hostname = 'localhost'; 
$username = 'Ciara'; 
$password = '0950767'; 
$database = 'users';   

$conn = mysqli_connect($hostname,$username, $password, $database); 
if (!$conn) { 
    die ("Databas connect failed: " . mysqli_connect_error()); 
} else { 
    echo "Connected successfully. <br>"; 
} 

?> 
<html> 
    <head> 
     <title>Register</title> 
    </head> 
    <body><center> 
     <form method='post' action='<?php echo $_SERVER["PHP_SELF"];?>'> 
     Firstname: <input type='text' name='firstname'><br> 
     Lastname: <input type='text' name='lastname'><br> 
     E-mail: <input type='text' name='email'><br> 
     Gender: <input type='radio' value='female' name='gender'>Female 
       <input type='radio' value='male' name='gender'>Male<br> 
     Username: <input type='text' name='username'><br> 
     Password: <input type='text' name='password'><br> 
     <input type='submit' value='Submit Form'> 
     </form></center> 

<?php 
if (isset($_POST['firstname']) && 
    isset($_POST['lastname']) && 
    isset($_POST['email']) && 
    isset($_POST['gender']) && 
    isset($_POST['username']) && 
    isset($_POST['password'])) { 

    $firstname = get($conn, 'firstname'); 
    $lastname = get($conn, 'lastname'); 
    $email = get($conn, 'email'); 
    $gender = get($conn, 'gender'); 
    $username = get($conn, 'username'); 
    $password = get($conn, 'password'); 

    $query = "insert into useracc values" . "(null, '$firstname','$lastname','$email','$gender','$username','$password')"; 

    if (mysqli_query($conn, $query)) { 
     echo "You have successfully registered! <br>"; 
    } else { 
     echo "Failed to register." . mysqli_error($conn); 
    } 
} 

mysqli_close($conn); 
echo "</body></html>"; 

function get($conn, $var) { 
    return mysqli_real_escape_string($conn, $_POST[$var]); 
} 
?> 

檢查這個代碼:)你不能在HTML中增加PHP。

相關問題