在HTML中运行PHP错误(此页面无法正常工作)

时间:2019-06-13 16:44:12

标签: php html mysql sql database

我为我的网站设置了此注册表单。人们输入他们的用户名,电子邮件和密码,然后使用php将其添加到我的数据库中。但是在运行代码时,我总是收到此错误。我的html文件和此PHP文件都在我的AWS服务器上,因此我认为代码中肯定有错误。我对PHP还是很陌生。

HTML:

<form method="get" action="signup_form.php">
    <input style="width: 300px; display: block; margin-left: auto; margin-right: auto;" type="text" name="signup_name" placeholder="Screen Name">
    <br>
    <input style="width: 300px; display: block; margin-left: auto; margin-right: auto;" type="text" name="signup_mail" placeholder="Your E-mail">
    <br>
    <input style="width: 300px; display: block; margin-left: auto; margin-right: auto;" type="password" name="signup_password" id = "password" placeholder="Create Password" required>
    <br>
    <input style="width: 300px; display: block; margin-left: auto; margin-right: auto;" type="password" name="confirm_password" id = "confirm_password" placeholder="Repeat Password" required>
    <br>
    <br>
    <button onclick="validatePassword()" class="button" style="display: block; margin-left: auto; margin-right: auto;" type="submit" name="submit_login">
        SUBMIT
    </button>


</form>

这是我的PHP代码:

<?php
$signup_name = filter_input(INPUT_GET, 'signup_name');
$signup_mail = filter_input(INPUT_GET, 'signup_mail');
$signup_password = filter_input(INPUT_GET, 'signup_password');

if (!empty($signup_name)){
if (!empty($signup_mail)){
    $host = "wildwea.......onaws.com";
    $dbusername = "a....in";
    $dbpassword = ".Bi....4.";
    $dbname = "innodb";
 // Create connection
    $conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
    if (mysqli_connect_error()){
        die('Connect Error ('. mysqli_connect_errno() .') '
            . mysqli_connect_error());
    }
    else{

        $sql = "SELECT EXISTS (SELECT 1 FROM Users WHERE Email = $signup_mail);"
        if ($sql = 0){

            $sql = "INSERT INTO Users (Username, Email, Pword)
            values ('$signup_name', '$signup_mail',md5('$signup_password'))";

            if ($conn->query($sql)){
                echo "New record is inserted sucessfully";
            }
            else{
                echo "Error: ". $sql ."
                ". $conn->error;
            }
            $conn->close();
        } else {
            echo "User already in database";
        }
    }
}
else{
    echo "Password should not be empty";
    die();
}
}
else{
    echo "Username should not be empty";
    die();
}
?>

如果您想查看错误,请访问注册页面链接: http://thewildwear.com/signup.html

1 个答案:

答案 0 :(得分:1)

我们看不到您的特定错误(看起来可能有多个错误),因此我们将无法为您提供帮助。但是我可以提出有关如何构建脚本的建议。

注意事项-对于最小的应用程序或学习而言,这实际上不是一种好方法。

主要思想是只有一个脚本,并且具有 processing display 部分。仅在实际提交表单时,它才会进入 processing 部分。

如果有任何验证错误,它将进入 display 部分,并列出错误和表格。

如果没有验证错误,它将保存到数据库并重定向到其他页面。

当您开发更大(更好)的应用程序时,您可能会发现这种类型的编码将很快变得笨拙-您将验证,SQL,视图/显示等混合在一个脚本中。它们将变得更加复杂,不久之后您将拥有一大团意大利面。一旦达到这一点,就开始研究框架。

但是现在,继续前进。祝好运。

<?php

// A list of validation errors. Initialize to an empty list.
$errors = [];

/****************************/
/******** PROCESSING ********/
/****************************/
// The form was submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // Values submitted from form
    $name = $_POST['signup_name'];
    $email = $_POST['signup_mail'];
    $password = $_POST['signup_password'];

    // Validation
    if (empty($name)) {
        $errors[] = 'Please enter your name';
    }

    if (empty($email)) {
        $errors[] = 'Please enter your email';
    }

    // ... check if email already exists in your DB.

    // ... more validation here

    // There are no validation errors, process the form.
    if (empty($errors)) {
        // At this point, you now have a valid form. Just save it to the DB.

        // Redirect to somewhere
    }
}

/****************************/
/********** DISPLAY *********/
/****************************/
if (count($errors) > 0) : ?>
    <ul>
        <?php foreach ($errors as $error): ?>
            <li><?php echo $error; ?></li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

<!-- Use "post" and remove action and it will post to itself. -->
<form method="post">
    <!-- ... -->