[英]data is not inserted in my MySQL database
我尝试在我的网站中实施时事通讯订阅表格。 由于我在编码方面很笨,我尝试将以下代码集成到我的网站中,我在这里找到了: http : //www.designerfreesolutions.com/resources/ajaxsignup_php/index.php
HTML代码:
<form onsubmit="return signup(this);return false;" method="post" name="subform" id="subform" action="optIn.php">
<div><span style="FONT-FAMILY: Arial; FONT-SIZE: 12pt; font-weight:bold;">Subscribe to our newsletter</span></div>
<div style="margin-top:20px">
<div>
<label style="display: inline-block;width:135px">Email:</label>
<input type="text" id="email" name="email" value="">
</div>
<div>
<label style="display: inline-block;width:135px">Name:</label>
<input type="text" name="name" id="name" value="">
</div>
<div>
<div style="display:inline-block;width:135px;"> </div>
<input type="submit" id="submit" name="submit" value="Sign up">
</div>
<div style="width:100%"><span id="Error" style="color:red;display:none;"></span></div>
<div id="myResponse" style="DISPLAY:none;"></div>
<div id="loading" style="display:none;"><img src="wait.gif" alt=""></div>
</div>
</form>
我以与下载相同的方式上传了 optIn.php 文件并更改了服务器/主机、数据库、用户名、密码信息:
<?php
//ini_set('display_errors', 0);
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
$email = trim(htmlspecialchars($_REQUEST["email"]));
$name = trim(htmlspecialchars($_REQUEST["name"]));
$pfileName = "mails.txt";
$MyFile = fopen($pfileName, "a");
$nline="\"".$email."\"" ."," ."\"".$name."\"" ."\r\n";
fwrite($MyFile, $nline);
fclose($MyFile);
die;
// *************** THE DATABASE CODE
//Attention: you may want to use stronger function here to
//purify the requested parameters and protect against injections.
//Example: $email = clean_this($_REQUEST["email"]);
$email = trim(htmlspecialchars($_REQUEST["email"]));
$name = trim(htmlspecialchars($_REQUEST["name"]));
// Lets connect to mySQL ==> replace with your own values for Server/Host, database, username, password
$pdbHost = "localhost";
$pdbUserName = "root";
$pdbPassword = "root";
$pdbName ="newsletters";
// You can use a different table (and fields for name and email).
// But change the table name and field names in the SQL queries below.
// connect to mySQL
$conlink = mysql_connect($pdbHost, $pdbUserName, $pdbPassword);
if(!$conlink) {die('<span class=errormessage>Unable to connect to '.$pdbHost.'</span><br>');}
mysql_select_db($pdbName, $conlink);
// Check if subscriber exists already
$SQL= "select email from mysubscribers where email='".$email."'";
$result = mysql_query($SQL);
if(!$result) {die('Problem in SQL: '.$SQL);} //just ccking if there was a problem with your query
if (mysql_num_rows($result)==0) { // it's a new email=> add it
$SQL2= "INSERT into mysubscribers (name, email) VALUES ('".$name."', '".$email."')";
mysql_query($SQL2);
}
mysql_close($conlink);
//Sample script for the table:mysubscribers in the database:newsletters
/*
CREATE TABLE `mysubscribers` (
`idEmail` mediumint(9) NOT NULL auto_increment,
`email` varchar(150) default NULL,
`name` varchar(150) default NULL,
PRIMARY KEY (`idEmail`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/
?>
现在,无论我尝试什么,数据都没有插入到我的 mysql 数据库中。 我创建了代码中指定的表......有没有人知道我做错了什么? 此外,我想向订阅者发送一封确认邮件..如果有人有一些代码给我,那就太好了......注意:我不是专业程序员..我试着用一些可用的代码来帮助自己在互联网上查找 :s 感谢您的帮助! 安娜
代码退出die命令。 如果要将数据存储到 mysql,则应删除这些行
$email = trim(htmlspecialchars($_REQUEST["email"]));
$name = trim(htmlspecialchars($_REQUEST["name"]));
$pfileName = "mails.txt";
$MyFile = fopen($pfileName, "a");
$nline="\"".$email."\"" ."," ."\"".$name."\"" ."\r\n";
fwrite($MyFile, $nline);
fclose($MyFile);
die;
对于发送电子邮件,你应该尝试一些像phpmailer这样的 php 库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.