双文件PHP联系表单

时间:2011-04-19 03:11:36

标签: php mysql forms contact

psw index.php部分似乎工作正常,但处理程序抛出内部服务器错误(500)。

的index.php

    <div id="contact">
        <h1>Contact us</h1>
        <form id="ContactForm" method="post" action="submit.php">
            <p>
                <label>Name<span>(optional)</span></label>
                <input id="name" name="name" class="inplaceError" maxlength="120" type="text" autocomplete="on"/>`enter code here`
                <span class="error" style="display:none;"></span>
            </p>
            <p>
                <label>Email<span>(optional)</span></label>
                <input id="email" name="email" type="text" />
            </p>
            <p>
                <label>Subject<span>(optional)</span></label>
                <input id="website" name="website" />
            </p>
            <p>
                <label>Your message<br /> <span>700 characters allowed</span></label>
                <textarea id="message" name="message" cols="6" rows="5"></textarea>
            </p>
            <p class="submit">
                <input id="send" type="submit" value="Submit" name="submit"/>
                <span id="loader" class="loader" style="display:none;"></span>
            </p>
    <input id="newcontact" name="newcontact" type="hidden" value="1"></input>
        </form>

submit.php -->


if(isset($_POST['submit'])) {
   $name = $_POST['name'];
   $email = $_POST['email'];
   $website = $_POST['website']; //subject
   $message = $_POST['message'];

   $connection = mysql_connect ("localhost", "root", "pswd") or die ('DB connection fail:' .mysql_error());
   mysql_select_db ("contactform");
   $query = "INSERT INTO contact (pk_contact, name, email, website, message, added_date)VALUES ('NULL','$name','$email','$sub','$msg','NULL')";
   //good to know about the quotes, but the page still insist there's an error
   mysql_query($query) or die ('Error uploading DB');
   mysql_close($connection);
   echo "<span id="success_message" class="success">YAY! It worked.</span>";
} else {
   echo "<span class='error'>Try again.</span>";
}

2 个答案:

答案 0 :(得分:2)

我认为您的查询字符串出错了。它应该看起来像

$query = "INSERT INTO contact (name, email, website, message, added_date)VALUES ('$name','$email','$sub','$msg',NULL)";

双引号分解你的字符串,不同的部分没有被连接。

答案 1 :(得分:0)

正如迈克西托所说。您需要删除NULL周围的引号。

 $query = "INSERT INTO contact (pk_contact, name, email, website, message, added_date)VALUES (NULL,'$name','$email','$sub','$msg',NULL)";

假设您已设置mysql表,因此added_date字段是mysql时间戳格式,尝试在表中输入'NULL'将导致mysql错误。

还要确保mysql表允许在pk_contactadded_date字段中输入空值。