Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 未保存到mysql的电子邮件_Php_Mysql_Email_Insert - Fatal编程技术网

Php 未保存到mysql的电子邮件

Php 未保存到mysql的电子邮件,php,mysql,email,insert,Php,Mysql,Email,Insert,嗨,我想知道是否有人可以看看我的代码,看看有什么错误,因为我看不到任何。现在发生的事情是,它没有保存我发送给mysql的电子邮件,相反,我收到了一封退回的电子邮件 当我运行PHP测试以查看它是否保存到mysql表时,它不会 我已经取出了连接代码,因为它有我的用户名和密码 #!/usr/bin/php -q <?php mysql_connect("123.123.123.2", "abc_ard", "5555") or die(mysql_error()); mysql_select_d

嗨,我想知道是否有人可以看看我的代码,看看有什么错误,因为我看不到任何。现在发生的事情是,它没有保存我发送给mysql的电子邮件,相反,我收到了一封退回的电子邮件

当我运行PHP测试以查看它是否保存到mysql表时,它不会

我已经取出了连接代码,因为它有我的用户名和密码

#!/usr/bin/php -q
<?php
mysql_connect("123.123.123.2", "abc_ard", "5555") or die(mysql_error());
mysql_select_db("55_servermail") or die(mysql_error());

chdir(dirname(__FILE__));
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
    $email .= fread($fd, 1024);
}
fclose($fd);

if(strlen($email)<1) {
    die(); 
}

// handle email
$lines = explode("\n", $email);

// empty vars
$from = "";
$to="";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;

for ($i=0; $i < count($lines); $i++) {
    if ($splittingheaders) {
        // this is a header
        $headers .= $lines[$i]."\n";
        // look out for special headers
        if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
            $subject = $matches[1];
        }
        if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
            $from = $matches[1];
        }
        if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
            $to = $matches[1];
        }
    } else {
        // not a header, but message
        $message .= $lines[$i]."\n";
    }
    if (trim($lines[$i])=="") {
        // empty line, header section has ended
        $splittingheaders = false;
    }
}


mysql_query("INSERT INTO mail
(`to`,`from`,`header`,`subject`,`msg`,`original`)
VALUES
('{$to}','{$from}', '{$headers}','{$subject}','{$message}','{$email}')") or die(mysql_error());;


?>

我在当前代码中可能看到的唯一错误是,可能会输入诸如撇号之类的字符“请验证,如果希望它包含在消息正文或其他字段中,请确保使用反斜杠,例如\”,以便mysql将其分析为字符,而不是结束撇号,但是,为了避免sql注入,您必须采取安全措施:

在向mysql添加信息时,您必须考虑mysql和PHP使用的字符,并对它们进行寻址,因为它们可能导致代码失败,甚至允许人们在您的站点上插入和/或执行代码。我使用的最简单的方法是让PHP对字符进行转义,以允许它们正确插入mysql,如下所示:

$email = "This isn't my first time 'round here"; 
这应该可以按原样插入,但假设您的SQL如下所示:

$query = "INSERT INTO table (timestamp,email) VALUES (time(),'$email')";
MySQL查询中的单引号将被数据中的单引号抬高。为了避免这种情况,请将电子邮件变量设置为转义:

$email = "This isn't my first time 'round here"; 
$email = mysql_escape_string($email);
使用这种方法的关键是,现在数据库中有额外的转义字符,通常是数据中的反斜杠,因此,当您要从数据库中使用这些字符时,必须将其删除,PHP有一个方便的函数用于此:

    $query = "SELECT * FROM table WHERE id='the id you want'"; 
    $result = mysql_query($query) or die(mysql_error());
    while ($data = mysql_fetch_array($result)) {
        $email = stripslashes($data['email']);
    }

    echo "This is your email text from the database: <br>\n".$email."<br>\n";

如果这确实是电子邮件未按预期插入数据库的原因,希望这有助于澄清一个可能的解决方案。

您能打印出查询文本并将其直接输入mysql数据库吗?你的话很不清楚。你能把你要放在邮件字段上的样本值贴出来吗?