data is not inserted in my MySQL database

时间:2015-10-29 15:48:51

标签: php mysql database newsletter

I try to implement a newsletter subscription form into my website. As I am quite dummy in coding, I try to integrate following code into my site, which I found here: http://www.designerfreesolutions.com/resources/ajaxsignup_php/index.php

HTML code:

<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;">&nbsp;</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>

I uploaded the optIn.php file in the same way as in the download and changed the Server/Host, database, username, password info :

<?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; 
*/
?>

Now, whatever I try, the data is not inserted in my mysql database. I created the tables as specified in the code... Does anyone have any idea what I am doing wrong? Moreover, I would like to send the subscriber a confirmation mail.. If anyone has some code for me, that would be great... Note: I am not a professional programmer.. I try to help myself with some available code I can find on the internet :s Thanks for your help! Anna

1 个答案:

答案 0 :(得分:0)

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库