【问题标题】:Could someone help figure out the problems with the statements in this PHP file?有人可以帮助找出这个 PHP 文件中的语句的问题吗?
【发布时间】:2011-08-09 14:16:58
【问题描述】:
<?php
$con = mysql_connect("localhost", "root");

if (!$con)
{
die("Cannot make a connection");
}

$customer_first_name = $_POST['customer_first_name'];
$customer_last_name = $_POST['customer_last_name'];
$customer_email = $_POST['customer_email'];
$category_id = $_POST['category_id'];
$problem_body = $_POST['problem_body'];

mysql_select_db('yumbox_table', $con) or die(mysql_error());

mysql_query("INSERT INTO yumbox_customer_inquiry (customer_last_name, customer_first_name, customer_email, category_id, problem_body) values ($customer_last_name, $customer_first_name, $customer_email, $category_id, $problem_body)", $con);


if ($category_id==1)
{
mail('technical_problems_11@yahoo.com… 'You have a new email from $customer_first_name $customer_last_name at $customer_email', $problem_body);
}

if ($category_id==2)
{
mail('login_problems11@yahoo.com', 'You have a new email from $firstname $lastname at $emailaddress', $problem_body);
}

if ($category_id==3)
{
mail('order_problems_11@yahoo.com', 'You have a new email from $firstname $lastname at $emailaddress', $problem_body);
}

echo('Thank you for sending us your feedback. A customer support representative will respond to you shortly');




mysql_close($con)
?>

在一个 html 页面中,假设用户在表单中输入信息,并且假设数据进入这个 php 文件。假设从这里将其存入 mysql 表,并且该数据也假设发送到电子邮件地址。但是,它不会将代码发送到 mysql 表,并且您会收到一条错误消息,指出它不能通过电子邮件发送。有人可以帮我解决这个莫名其妙的问题吗?

【问题讨论】:

  • 代码中的一些缩进会大有帮助。您应该使用 mysql_real_escape_string($query) 来阻止注入攻击,因为您使用的是来自 $_POST 的不受信任的输入

标签: php mysql html database email


【解决方案1】:

除了 Adnan 的帖子,我还建议使用变量捕获查询结果,并在完成后关闭 MySQL 连接:

$result = mysql_query(...);
// Evaluate the result, if its false the query failed.
if($result == FALSE && $debugoutput == TRUE)
{
  //This should be debug output only!  
  //It could potentially expose your code to the public
  $errorNum = "MySQL Error Number: " . mysql_errno($conn);
  $errorMsg = "MySQL Error Message: " . mysql_error($conn);
}

mysql_close($con);

至于您的电子邮件问题,您是否设置了邮件服务器?如果是这样,它是否正确配置为接收来自 PHP 的请求?很抱歉,我还没有太多使用邮件服务器和 PHP 的经验……但看起来并不难。

也许这对我来说有点“挑剔”,但我喜欢将功能分成不同的区域:有一种方法可以将数据存储在您的数据库中,并有一种单独的方法可以发送您的电子邮件。它还可以通过查看它是数据库问题还是电子邮件问题或两者兼而有之来帮助您调试当前问题?只是说...

【讨论】:

  • 这是我听过的最好的答案..永远
【解决方案2】:

包括单引号:

('$customer_last_name', '$customer_first_name', '$customer_email', '$category_id', '$problem_body')

注意:如果category_id 在您的表中是数字,则不需要引号。

另外,至少清理发布的数据:

$customer_first_name = mysql_escape_string($_POST['customer_first_name']);
... and so on

这将保护您的数据库免受 SQL 注入。

【讨论】:

    【解决方案3】:
    1. 您对 SQL 注入持开放态度。见Bobby Tables
    2. 您的查询没有错误检查。如果失败,他们返回 FALSE。作为最低限度,您应该:

      mysql_query($query) or die(mysql_error());

    3. 您的示例代码没有任何“无法通过电子邮件发送”错误类型的文本,您在哪里看到的?

    【讨论】:

      猜你喜欢
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多