PHP:$ variable导致php代码中断/退出(?)

时间:2017-09-03 18:18:28

标签: php

我正在编写我的第一个php应用程序,我正在根据我正在处理的有用的教程。我现在的代码工作正常,直到我进入$ var = $ connection->查询(" INSERT INTO ...等等。

此时,紧接在第一个$之后的代码在firefox中显示为纯文本。 (google将整个内容显示为文本等等)。

我会在这里发布我的代码:

<?php 



$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "cowboyserver";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword);
mysqli_select_db($dbName, $conn);


$email = ($_POST['email']);

if(!$conn){
    echo 'error';
}else{
    $query = $conn->query("INSERT INTO email_list  (email) VALUES ('$email')");
}
mysqli_query($query);
header("Location: ../index.html?signup=success");
echo '<p>email entered !! ! ! ! ! ! !! !! ! ! ! ! !</p>'    ;

此外,这里是HTML ::::

<form autocomplete="on" action="includes/signup.inc.php" method="POST">
    <input type="email" name="email" placeholder="put your email here" class="blah"/>
</form>

编辑: 在尝试了一些解决方案后,我发现我的PHP代码在代码中看似随机的点处中断。例如,在发布的第二个答案中,php代码一直运行,直到它变为&#34; $ conn-&gt; connect_error&#34;在if语句中然后打印出 - &gt;之后的所有内容而不是执行它。

2 个答案:

答案 0 :(得分:3)

您需要的更改: -

1.需要将文件名从signup.php更改为<form autocomplete="on" action="includes/signup.php" method="POST"> <input type="email" name="email" placeholder="put your email here" class="blah"/> </form> ,然后在下面使用它: -

signup.php

2.更改 <?php //comment these two lines when code executed successfully error_reporting(E_ALL); ini_set('display_errors',1); if(!empty($_POST['email']){ // check posted data coming or not $dbServername = "localhost"; $dbUsername = "root"; $dbPassword = ""; $dbName = "cowboyserver"; $conn = mysqli_connect($dbServername, $dbUsername, $dbPassword,$dbName); //add dbname here itself //check conneced or not if(!$conn){ // $ missed die('connection problem'.mysqli_connect_error());//check for real connection problem }else{ $email = $_POST['email'];// remove () //don't mix oop way to procedural way and use prepared statements $stmt = mysqli_prepare($conn, "INSERT INTO email_list (email) VALUES (?)")); /* bind parameters for markers */ mysqli_stmt_bind_param($stmt, "s", $email); /* execute query */ if(mysqli_stmt_execute($stmt)){//check query executes or not header("Location: ../index.html?signup=success"); echo '<p>email entered !! ! ! ! ! ! !! !! ! ! ! ! !</p>'; exit(); }else{ echo "insersion failde because of".mysqli_error($conn); } } }else{ echo "please fill the form"; } (您重命名的文件) 代码(更改已注释): -

prepared statements

注意: - 始终使用SQL INJECTION来阻止DemoMVCProj.proj。谢谢

答案 1 :(得分:2)

试试这个。将form更改为包含submit按钮。然后,只有您可以使用$_POST访问值。

<form autocomplete="on" action="includes/signup.php" method="POST">
    <input type="email" name="email" placeholder="put your email here" class="blah"/>
    <input type="submit" value="Form Submission" name="submitBtn">
</form>

您的signup.php页面:

<?php

$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "cowboyserver";

// Create connection
$conn = new mysqli($conn = new mysqli($dbServername, $dbUsername, $dbPassword, $dbName));
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if(isset($_POST['submitBtn'])) { //form submission occured

    $email = $_POST['email'];
    $sql = "INSERT INTO email_list (email) VALUES ('$email')";

    if ($conn->query($sql)) {
        echo '<p>email entered !! ! ! ! ! ! !! !! ! ! ! ! !</p>';
        header("Location: ../index.html?signup=success");
        exit();

    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

} else {
    echo "Form Submission Error";
}

$conn->close();
?>

希望它有用。