我创建了这个表单:
<form method="post" action="process-form.php" id="emailForm" name="emailForm" target="_self">
<h4>Sign up to be notified when we go live!</h4>
<label for="email">E-mail</label>
<input type="text" name="email" id="email" />
<input type="submit" name="submit" id="submit" value="Submit" onclick="return alert('Thanks! Your email has been added.');">
<p>emails will not be shared with third parties</p>
</form>
使用以下php代码
<?php
//open database connect
$username = "username";
$password = "password";
$hostname = "server";
$conn = mysql_connect($hostname, $username, $password);
if (!$conn) die('Could not connect: ' . mysql_error());
//emaillist is the name of the database
mysql_select_db('emaillist');
//Clean data
function clean_data($string){
if (get_magic_quotes_gpc()){
$string = stripslashes($string);
}
$string = strip_tags($string);
return mysql_real_escape_string($string);
}
//Mail Header removal
function remove_headers($string){
$headers = array(
"/to:/i",
"/from:/i",
"/bcc:/i",
"/cc:/i",
"/Content-Transfer-Encoding:/i",
"/Content-Type:/i",
"/Mime-Version:/i",
);
$string = preg_replace($headers, '', $string);
return strip_tags($string);
}
//Pick up cleaned data
$email = clean_data($_POST['email']);
//Insert data
//emails is the name of the table
$query ="INSERT INTO emails (email)
VALUES ('$email')";
mysql_query($query);
//close Connection
mysql_close($conn);
//redirect to page
header('Location: defaultUpCyc.html');
?>
在我的测试环境中,一切都工作得很好,很好,当我把它投入使用的几天。然而,我去我的数据库看看是否有任何电子邮件提交,它完全是空的。我已经把测试邮件放入表格并检查了数据库,没有任何东西。自从上线后我就没做过任何改动所以我很困惑。下面是测试站点:http://upcycledonline.com/test/Site/defaultUpCyc.html
谢谢你的帮助!
您应该通过添加:
来检查您的连接是否正常if (!$conn) { die('Could not connect: ' . mysql_error()); }
还检查firebug以确保表单发送了数据。您还可以回显查询以检查格式。否则一切看起来都很好,所以我的假设是数据库连接不好。
只是猜测:)
测试站点上的表单标签没有定义其动作,这意味着它将数据发送给自己,并且HTML不能与数据库一起工作。
我不知道如何在这里添加代码,所以我做了一个粘贴:http://pastebin.com/baPUPhbt你可以使用
我认为你的问题是在那个onclick javascript,试着删除它,它可能工作