我有一个网站,其中有一个表单,可以将数据提交到数据库,还会向电子邮件地址发送一封电子邮件地址,该地址在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();
}
?>
答案 0 :(得分:0)
您可以尝试此查询。
"SELECT * FROM $tablename WHERE rand = '$rand'"
代替'SELECT * FROM $tablename WHERE rand = $rand'
如果它不工作,请检查您的表名,字段名称和变量。
答案 1 :(得分:0)
我会将其更改为以下代码。
更改了isset
,while ...
循环并删除了对$result
的错误检查。我还删除了您正在使用的urlencode
的调用,因为如果您要在屏幕上显示这些值,它们没有任何意义。在编码要在URL的查询部分中使用的字符串时,该函数很方便,这是将变量传递到下一页的便捷方式。请参阅here。
最后,在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;
}
}
?>