doufangyan6862 2016-02-05 22:49
浏览 169

从数据库获取电子邮件值并发送它(PHP电子邮件)/刷新页面而不更改网站链接

I Have 2 problems with this script, something is wrong 1. the email address value can't be selected from the database. the script works only if I manually type the e-mail

$yourEmail = "email@exemple.com";
  1. after I press the submit button, I want the page to refresh without the website link being changed or after i click send, the contact box should close. Could you please help to solve these problems? Thank you in advance!

            <?php
            $sql = "select * from tables where email='" . $email . "'";
            while($row=mysql_fetch_array($sql))
            {
            $email=$row['email'];
            }
            $yourEmail = $email; // the email address you wish to receive these mails through
            $yourWebsite = "WEBSITE NAME";
            $thanksPage = ''; // URL to 'thanks for sending mail' page; leave empty to keep                          message on the same page 
            $maxPoints = 4; 
            $requiredFields = "name,email,comments";
    
    
    
            $error_msg = array();
            $result = null;
    
            $requiredFields = explode(",", $requiredFields);
    
            function clean($data) {
                $data = trim(stripslashes(strip_tags($data)));
                return $data;
            }
            function isBot() {
                $bots = array("Indy", "Blaiz", "Java", "libwww-perl", "Python", "OutfoxBot", "User-Agent", "PycURL", "AlphaServer", "T8Abot", "Syntryx", "WinHttp", "WebBandit", "nicebot", "Teoma", "alexa", "froogle", "inktomi", "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory", "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot", "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz");
    
                foreach ($bots as $bot)
                    if (stripos($_SERVER['HTTP_USER_AGENT'], $bot) !== false)
                        return true;
    
                if (empty($_SERVER['HTTP_USER_AGENT']) || $_SERVER['HTTP_USER_AGENT'] == " ")
                    return true;
    
                return false;
            }
    
            if ($_SERVER['REQUEST_METHOD'] == "POST") {
                if (isBot() !== false)
                    $error_msg[] = "No bots please! UA reported as: ".$_SERVER['HTTP_USER_AGENT'];
    
                // lets check a few things - not enough to trigger an error on their own, but worth assigning a spam score.. 
                // score quickly adds up therefore allowing genuine users with 'accidental' score through but cutting out real spam :)
                $points = (int)0;
    
                $badwords = array("adult", "beastial", "bestial", "blowjob", "clit", "cum", "cunilingus", "cunillingus", "cunnilingus", "cunt", "ejaculate", "fag", "felatio", "fellatio", "fuck", "fuk", "fuks", "gangbang", "gangbanged", "gangbangs", "hotsex", "hardcode", "jism", "jiz", "orgasim", "orgasims", "orgasm", "orgasms", "phonesex", "phuk", "phuq", "pussies", "pussy", "spunk", "xxx", "viagra", "phentermine", "tramadol", "adipex", "advai", "alprazolam", "ambien", "ambian", "amoxicillin", "antivert", "blackjack", "backgammon", "texas", "holdem", "poker", "carisoprodol", "ciara", "ciprofloxacin", "debt", "dating", "porn", "link=", "voyeur", "content-type", "bcc:", "cc:", "document.cookie", "onclick", "onload", "javascript");
    
                foreach ($badwords as $word)
                    if (
                        strpos(strtolower($_POST['comments']), $word) !== false || 
                        strpos(strtolower($_POST['name']), $word) !== false
                    )
                        $points += 2;
    
                if (strpos($_POST['comments'], "http://") !== false || strpos($_POST['comments'], "www.") !== false)
                    $points += 2;
                if (isset($_POST['nojs']))
                    $points += 1;
                if (preg_match("/(<.*>)/i", $_POST['comments']))
                    $points += 2;
                if (strlen($_POST['name']) < 3)
                    $points += 1;
                if (strlen($_POST['comments']) < 15 || strlen($_POST['comments'] > 1500))
                    $points += 2;
                if (preg_match("/[bcdfghjklmnpqrstvwxyz]{7,}/i", $_POST['comments']))
                    $points += 1;
                // end score assignments
    
                foreach($requiredFields as $field) {
                    trim($_POST[$field]);
    
                    if (!isset($_POST[$field]) || empty($_POST[$field]) && array_pop($error_msg) != "Please fill in all the required fields and submit again.
    ")
                        $error_msg[] = "Please fill in all the required fields and submit again.";
                }
    
                if (!empty($_POST['name']) && !preg_match("/^[a-zA-Z-'\s]*$/", stripslashes($_POST['name'])))
                    $error_msg[] = "The name field must not contain special characters.
    ";
                if (!empty($_POST['email']) && !preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i', strtolower($_POST['email'])))
                    $error_msg[] = "That is not a valid e-mail address.
    ";
                if (!empty($_POST['url']) && !preg_match('/^(http|https):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i', $_POST['url']))
                    $error_msg[] = "Invalid website url.
    ";
    
                if ($error_msg == NULL && $points <= $maxPoints) {
                    $subject = "Automatic Form Email";
    
                    $message = "You received this e-mail message through your website: 
    
    ";
                    foreach ($_POST as $key => $val) {
                        if (is_array($val)) {
                            foreach ($val as $subval) {
                                $message .= ucwords($key) . ": " . clean($subval) . "
    ";
                            }
                        } else {
                            $message .= ucwords($key) . ": " . clean($val) . "
    ";
                        }
                    }
                    $message .= "
    ";
                    $message .= 'IP: '.$_SERVER['REMOTE_ADDR']."
    ";
                    $message .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."
    ";
                    $message .= 'Points: '.$points;
    
                    if (strstr($_SERVER['SERVER_SOFTWARE'], "Win")) {
                        $headers   = "From: $yourEmail
    ";
                    } else {
                        $headers   = "From: $yourWebsite <$yourEmail>
    "; 
                    }
                    $headers  .= "Reply-To: {$_POST['email']}
    ";
    
                    if (mail($yourEmail,$subject,$message,$headers)) {
                        if (!empty($thanksPage)) {
                            header("Location: $thanksPage");
                            exit;
                        } else {
                            $result = 'Your mail was successfully sent.';
                            $disable = true;
                        }
                    } else {
                        $error_msg[] = 'Your mail could not be sent this time. ['.$points.']';
                    }
                } else {
                    if (empty($error_msg))
                        $error_msg[] = 'Your mail looks too much like spam, and could not be sent this time. ['.$points.']';
                }
            }
            function get_data($var) {
                if (isset($_POST[$var]))
                    echo htmlspecialchars($_POST[$var]);
            }
            ?>
    

html form

<form action="<?php echo basename(__FILE__); ?>" method="post">
<noscript>
        <p><input type="hidden" name="nojs" id="nojs" /></p>
</noscript>
<p>
    <label for="name">Name: *</label> 
        <input type="text" name="name" id="name" value="<?php get_data("name"); ?>" /><br />

    <label for="email">E-mail: *</label> 
        <input type="text" name="email" id="email" value="<?php get_data("email"); ?>" /><br />

    <label for="url">Website URL:</label> 
        <input type="text" name="url" id="url" value="<?php get_data("url"); ?>" /><br />

    <label for="location">Location:</label>
        <input type="text" name="location" id="location" value="<?php get_data("location"); ?>" /><br />

    <label for="comments">Comments: *</label>
        <textarea name="comments" id="comments" rows="5" cols="20"><?php get_data("comments"); ?></textarea><br />
</p>
<p>
    <input type="submit" name="submit" id="submit" value="Send" <?php if (isset($disable) && $disable === true) echo ' disabled="disabled"'; ?> />
</p>
</form>
  • 写回答

1条回答 默认 最新

  • doumixiang2227 2016-02-05 23:25
    关注

    Before your while loop your suppose to actually perform the query with mysql_query or mysqli_query in your case mysql_query. Your other problem is your fetching the results as an array which is index based (0...) and your using a string to access it so you should call mysql_fetch_assoc which returns an associative array which can be accessed in your current implementation

     $sql = "select * from tables where email='" . $email . "'";
     $result = mysql_query($sql, $connection);// this actually performs the query and returns the result to be fetched using mysql_fetch_array or it's other methods such as mysql_fetch_assoc
     while($row=mysql_fetch_assoc($result)){
        $email=$row['email'];
     }
    

    Also declare your $email variable outside the while loop as if no rows were returned from the database your $email variable won't exist therefore breaking your code so you should bring it to the top of the while loop and initialize it to some default value.

    评论

报告相同问题?

悬赏问题

  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿