【发布时间】:2011-12-27 18:07:41
【问题描述】:
我创建了这个表单:
<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
感谢您的帮助!
【问题讨论】:
-
您的代码根本没有进行任何类型的错误报告,甚至没有检查事件是否成功。例如,mysql_connect 将返回一个数据库句柄,或者如果它无法连接到数据库,则返回 false。您没有检查 mysql_connect 是否返回了有效的数据库句柄,那么您的脚本应该如何知道它是否无法连接到数据库?代码中有许多点您没有检查操作是否成功,您的脚本可能在其中任何一点上失败。
-
我应该提到我对大部分内容都是新手。
-
附带说明,您应该开始使用 OO MySQLi 类,就像
allows you to access the functionality provided by MySQL 4.1 and above