douyimiao1993 2015-07-27 07:17
浏览 38
已采纳

在EC2上为S3托管网站运行PHP联系表单

I'm a fairly novice-level programmer, so please excuse any (very) banal mistakes in the following.

I'm currently creating a personal branding website of a few HTML5/CSS3 pages hosted on Amazon S3, including a contact page. My contact form currently directs to a PHP page on an (Amazon Linux) EC2 instance that I have setup as a LAMP server by following AWS's guide and some (kind of fun) trial-and-error. My intention is to get this PHP form to splice whatever information the contact puts in into a MySQL database hosted as a separate (AWS-based) RDS instance.

While I know that there's many more simple ways out there (a mailto action, etc.), I've been trying my hardest to get this to work, but can't seem to figure out what I'm doing wrong. My form will redirect to the PHP page perfectly fine, but no returns occur nor data put into the DB. Essentially, I just get a flat white page.

Must I set up the database on the same LAMP server as the PHP redirect, or is there something completely wonky in my code?

I've posted my PHP code below as well as the Contact page's form HTML. Any help would be greatly appreciated! Thank you so much in advance!

PHP Code (contact.php):

<?php

$link = mysql_connect("[Instance Public IP]:[Port #]", "[Username]", "[Password]", "[Database Name]")

if($link === false) {
    die("ERROR: Could not connect." . mysqli_connect_error());
}

$contact_name = mysqli_real_escape_string($link, $_POST['Name']);
$contact_email = mysqli_real_escape_string($link, $_POST['E-Mail']);
$subject = mysqli_real_escape_string($link, $_POST['Subject']);
$message = mysqli_real_escape_string($link, $_POST['Message']);

$sql = "INSERT INTO contacts (contact_name, contact_email, subject, message) VALUES ('$contact_name','$contact_email', '$subject', '$message')";
if(mysqli_query($link, $sql)){
    echo "Thank you! Your message has been sent successfully.";
} else{
    echo "ERROR: Unable to send successfully." .
    mysqli_error($link);
}

mysqli_close($link);
?>

HTML Code (contact.html):

            <form action="[contact.php URL]" method="post">
            <div>
                <label for="Name"><b>Name:</b></label>
                <input type="text" id="Name" autofocus required />
            </div>
            <div>
                <label for="E-Mail"><b>E-Mail:</b></label>
                <input type="email" id="E-Mail" required />
            </div>
            <div>
                <label for="Subject"><b>Subject:</b></label>
                <input type="text" id="Subject" required />
            </div>
            <div>
                <label for="Message"><b>Message:</b></label>
                <textarea id="Message" required>Please write your inquiry here.</textarea>
            </div>
            <div>
                <button type="submit">Submit</button>
            </div>
            <div>
                <button type="reset">Reset</button>
            </div>
        </form>

I have replaced all [generic tags] in my production code with the requisite username(s), password(s), etc.

  • 写回答

1条回答 默认 最新

  • dtvfxzq3802 2015-07-27 10:09
    关注

    So the error was:

    • Syntax errors
    • HTML input name attribute is needed for posting form in php

    Some suggestions:

    • Use a IDE that can auto detect your syntax errors.
    • Start learning simple debugging tricks like var_dump
    • Dive into xdebug

    Happy coding :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?