-2

我有一个网站,它有一个表单,可以将数据提交到数据库,还会向电子邮件地址发送电子邮件,该地址在 URL 末尾有一个随机 ID,它将提取特定数据并将其显示在网页上。基本上它是我正在做的情人节消息类型的事情

我已经将数据发送到数据库并完美存储,但由于某种奇怪的原因,我在提取数据时遇到了问题。

这是我的代码,有什么你可以看到可能导致问题的吗?

<?php

// since the id is being passed in the url, you will need to declare it using the get method
$rand = $_GET['rand'];
$action = $_GET['action'];

// if an id was sent to the script, then execute it
if ($rand)
{
   // connection vars
   $host = "localhost";
   $user = "***";
   $password = "***";
   $dbname = "***";
   $tablename = "cards";


   $mysql = new mysqli('$host, $user, $password');
   $result = $mysql->query('SELECT * FROM $tablename WHERE rand = $rand');

   while (($row = $result->fetch_assoc()) !== null) {
      print_r($row);

      $youremail = urlencode($row['youremail']);
      $name = urlencode($row['name']);
      $receiveremail = urlencode($row['receiveremail']);
      $message = $row['message'];

      // replace non flash line breaks with the flash \r newline
      $message = str_replace('\n', '\r', $message);
      $message = str_replace('\r\n', '\r', $message);
      $message = str_replace('<br>', '\r', $message);
      $message = str_replace('%0D%0A', '\r', $message);

   }

   // if there was a result echo the stuff below
   if($result)
   {
      // if we have a result we can show the movie and pass the vars along in the strings
  // a set back with this is that you can only pass so much data in the string, think its like 256 characters, but Im not sure.

      echo "Hello, $name <br />";
      echo "$message";
      exit();
   }
   mysql_close();
}
?>
4

2 回答 2

0

你可以试试这个查询。

"SELECT * FROM $tablename WHERE rand = '$rand'"反而'SELECT * FROM $tablename WHERE rand = $rand'

如果它不起作用,请检查您的表名、字段名和变量。

于 2013-02-06T14:02:50.977 回答
0

我会将其更改为下面的代码。

更改了您的isset, 您的while ...循环并删除了您对$result. 我还删除了您正在使用的调用urlencode,因为如果您要在屏幕上显示这些值,它们没有任何意义。在对要在 URL 的查询部分中使用的字符串进行编码时,该函数很方便,作为将变量传递到下一页的便捷方式。见这里

最后,去掉new mysqli()通话中的那些引号。

<?php

// since the id is being passed in the url, you will need to declare it using the get method
$action = $_GET['action'];

// if an id was sent to the script, then execute it
if (isset($_GET['rand'])) {
   $rand = $_GET['rand'];

   // connection vars
   $host = "localhost";
   $user = "***";
   $password = "***";
   $dbname = "***";
   $tablename = "cards";

   $mysql = new mysqli($host, $user, $password, $dbname);

   /* check connection */
   if (mysqli_connect_errno()) {
      printf("Connect failed: %s\n", mysqli_connect_error());
      exit();
   }

   $result = $mysql->query("SELECT * FROM $tablename WHERE rand = '$rand'");

   while ($row = $result->fetch_assoc()) {
      print_r($row);

      $youremail = $row['youremail'];
      $name = $row['name'];
      $receiveremail = $row['receiveremail'];
      $message = $row['message'];

      // replace non flash line breaks with the flash \r newline
      $message = str_replace('\n', '\r', $message);
      $message = str_replace('\r\n', '\r', $message);
      $message = str_replace('<br>', '\r', $message);
      $message = str_replace('%0D%0A', '\r', $message);

      echo "Hello, $name<br />";
      echo $message;

   }
}
?>
于 2013-02-06T14:22:18.427 回答