在PHP电子邮件表单正文中包含mySQL DB值
我使用PHP发送电子邮件,通过HTML联系人表单输入数据,代码如下: HTML在PHP电子邮件表单正文中包含mySQL DB值,php,mysql,email,Php,Mysql,Email,我使用PHP发送电子邮件,通过HTML联系人表单输入数据,代码如下: HTML 在if语句中,但是当通过PHP发送电子邮件时,$item_name变量仍然不显示任何内容?关闭-您太直接地盯着它看了。请注意,您请求的是请求参数,而不是获取的参数-请尝试以下操作: 我最初建议这样做,没有看到树的林,它清除了冗余和错误的XSS注入!问题,但仍然失败: <?php require 'connect.php'; $id = $_GET['id']; $id = mysql_real_escape_
在if语句中,但是当通过PHP发送电子邮件时,$item_name变量仍然不显示任何内容?关闭-您太直接地盯着它看了。请注意,您请求的是请求参数,而不是获取的参数-请尝试以下操作: 我最初建议这样做,没有看到树的林,它清除了冗余和错误的XSS注入!问题,但仍然失败:
<?php
require 'connect.php';
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `items` WHERE `item_id`='" . $id . "'";
$result = mysql_query($query);
while($fetch = mysql_fetch_assoc($result)){
//these variables only exist in this scope
$item_id = $fetch['item_id'];
$item_name = $fetch['item_name'];
//assuming you've found what you're looking for, terminate the loop, conditionally
if ($item_id != null && $item_name != null)
{
break;
}
}
if(isset($_POST['submit']))
{
$to="testemail@testemail.com";
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
//$item_name=$_REQUEST['item_name'];
$subject="New Message";
$body="Name: $name \n\n Email Address: $email \n\n Item Name: $item_name \n\n";
$sent=mail($to, $subject, $body);
echo 'Sent'; die;
}
相反,问题的主要原因是范围界定,请尝试这样做:
<?php
require 'connect.php';
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `items` WHERE `item_id`='" . $id . "'";
$result = mysql_query($query);
// placeholder variables in the scope you are interested in!
$item_id = null;
$item_name = null;
while($fetch = mysql_fetch_assoc($result)){
// now these reference a different (parent) scope
$item_id = $fetch['item_id'];
$item_name = $fetch['item_name'];
//assuming you've found what you're looking for, terminate the loop, conditionally
if ($item_id != null && $item_name != null)
{
break;
}
}
if(isset($_POST['submit']))
{
$to="testemail@testemail.com";
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
//$item_name=$_REQUEST['item_name'];
$subject="New Message";
$body="Name: $name \n\n Email Address: $email \n\n Item Name: $item_name \n\n";
$sent=mail($to, $subject, $body);
echo 'Sent'; die;
}
基本上,$item_uid[name]只存在于while循环中。当你在别的地方要它们的时候,它们是可用的吗?我没有答案,因为在这个问题规范中,我们没有答案
TL:DR~您的$item_uid[id | name]变量,其作用域在while循环的每个迭代中,例如本例中
建立一个更高级别的变量,它将被引用并设置值
另外-您最初是在if语句中重新编写$item_name变量
编辑30亿:该文档在:立体声安装说明中有点枯燥,但作为一个使用20多种不同语言开发了15年的人,他学会了热爱单元测试
phpunit是一套相当不错的套房。有一本手册,有一条学习曲线,但如果这是你的目标,它将使你成为一名更好的程序员-我绝对不希望听起来有屈尊俯就的感觉-很多时候人们都在“编程”以满足特定的要求并节省他们公司的钱-没有伤害没有犯规
单元测试让我每天都变得更好
不要使用$item\u name=$\u请求['item\u name'];在isset函数中,您将从select语句中获得实际的$item\u名称。您的代码中有2个$item\u名称声明:
$item_name = $fetch['item_name'];
及
因此,第二个将覆盖第一个。因此,如果没有for$\u请求['item\u name'],它将为空
要解决此问题,请删除第二个声明,以便$item_name将保存从数据库获取的值
注:
mysql_*函数在PHP5.5之后被弃用,在PHP7中被删除,请改用mysqli_*函数或PDO。mysql_*函数在PHP5.5之后被弃用,在PHP7中被删除,使用mysqli_*函数或PDO代替尝试使用var_dump$item_name如果有数据返回var_dump$item_name显示正确的值关于弃用的注意事项-但是作者没有提到它们是否在PHP7上,yetI已经删除了$item_name=$\u请求['item_name'];但是电子邮件$body中的$item_name变量仍然没有显示在已发送的电子邮件上?如果我在页面的其他地方回显$item_name,则会显示DB中的正确值?啊,所以我不知道您的DB数据-但可能发生的情况是,您正在获取并覆盖while循环中的变量。一旦设置了值,您就应该终止循环-我更新了我的示例~否则您将面临迭代/过去/变量并将其重置为null的风险-我使用了return而不是break-这些天有太多语言。使用break-updated这个例子谢谢,我已经试过了,但它并没有解决问题。我在页面的不同位置重复了变量,它们显示得很好,但在$body中却没有?我想是因为我没有使用echo$item_name?嗯-实际上,你分配$body的方式应该很好,我说“好”,因为它很可怕但很实用,应该可以使用nps,我看不出你的文本插值有任何问题。哈哈,伙计,我需要咖啡-请参阅更新的example@MikeE这些答案对你有帮助吗?接受最好的,谢谢:
$item_name = $_REQUEST['item_name'];
<?php
require 'connect.php';
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `items` WHERE `item_id`='" . $id . "'";
$result = mysql_query($query);
while($fetch = mysql_fetch_assoc($result)){
//these variables only exist in this scope
$item_id = $fetch['item_id'];
$item_name = $fetch['item_name'];
//assuming you've found what you're looking for, terminate the loop, conditionally
if ($item_id != null && $item_name != null)
{
break;
}
}
if(isset($_POST['submit']))
{
$to="testemail@testemail.com";
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
//$item_name=$_REQUEST['item_name'];
$subject="New Message";
$body="Name: $name \n\n Email Address: $email \n\n Item Name: $item_name \n\n";
$sent=mail($to, $subject, $body);
echo 'Sent'; die;
}
<?php
require 'connect.php';
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `items` WHERE `item_id`='" . $id . "'";
$result = mysql_query($query);
// placeholder variables in the scope you are interested in!
$item_id = null;
$item_name = null;
while($fetch = mysql_fetch_assoc($result)){
// now these reference a different (parent) scope
$item_id = $fetch['item_id'];
$item_name = $fetch['item_name'];
//assuming you've found what you're looking for, terminate the loop, conditionally
if ($item_id != null && $item_name != null)
{
break;
}
}
if(isset($_POST['submit']))
{
$to="testemail@testemail.com";
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
//$item_name=$_REQUEST['item_name'];
$subject="New Message";
$body="Name: $name \n\n Email Address: $email \n\n Item Name: $item_name \n\n";
$sent=mail($to, $subject, $body);
echo 'Sent'; die;
}
$item_name = $fetch['item_name'];
$item_name = $_REQUEST['item_name'];