解释
我将试图说明我的问题。(想跳过吗?跳到“这是我的问题”)这里有一个类似于我的MySQL表。
| NAME | PASSWORD | FOLLOWING
| Johny | XieofEnfoEQ | NULL
| Isabel | nfOEnfoiJEJj | NULL 我正在尝试创建一个php代码,允许用户跟踪其他用户。该系统遵循以下步骤:
1)连接到数据库。
2)获取当前用户所遵循的内爆数组(内嵌数组本质上是将数组转换为字符串)
3)将该字符串重新引爆到数组中。
4)添加所请求的用户
5)将数组重新内爆为字符串。
6)将当前用户的“以下”单元格更新为新的内爆字符串
7)给被请求的用户发一封电子邮件,说他们有了一个新的追随者
,这是我的问题,:假设我以Johny身份登录。当我试图跟踪伊莎贝尔(例如)时,它不会给我任何错误,而伊莎贝尔会收到电子邮件。但是,当我检查数据库时,下面的单元格仍然是空的。我该怎么解决这个问题?我的整个代码都在下面,删除了重要的值。
码
<?php
// ensure that they are logged in
require_once("./include/membersite_config.php");
require_once "Mail.php";
if(!$fgmembersite->CheckLogin()) {
$fgmembersite->RedirectToURL("http://####BLANKED####/HomeworkBox/Signup/login.php");
exit;
}
$searched="search";
// check the URL to fetch variables
if (isset($_GET['search'])) { // fetch what to send back to the search
$searched = $_GET['search'];
}
if (isset($_GET['follow'])) { // fetch who to follow
$follow = $_GET['follow'];
}
else {
header('Location: http://####BLANKED####/engine/search.php?search='.$searched);
die;
}
$member = $fgmembersite->UserFullName();
// check if this is a valid
$username="####BLANKED####";
$password="####BLANKED####";
$database="####BLANKED####";
$conn = mysql_connect("localhost",$username,$password)or die();
@mysql_select_db($database)or die();
$id = 0;
$result = mysql_query("SELECT * FROM members WHERE `name` = '$follow'");
if ($result && mysql_num_rows($result) > 0) {
}
else {
header('Location: http://####BLANKED####/engine/search.php?search='.$searched);
die;
}
$follow_data = mysql_fetch_row($result);
$follow_email = $follow_data[3];
$id = 0;
$result = mysql_query("SELECT * FROM members WHERE `name` = '$member'");
if ($result && mysql_num_rows($result) > 0) {
}
else {
header('Location: http://####BLANKED####/engine/search.php?search='.$searched);
die;
}
$member_data = mysql_fetch_row($result);
$member_email = $member_data[3];
$member_following = $member_data[14];
$following_list = explode(',', $member_following);
$following_list[] = $member;
$member_following = implode(",", $following_list);
$member_refined = str_replace(' ', '%20', $member);
mysql_query("UPDATE members SET following=$member_following WHERE email='$member_email'");
$from = '<####BLANKED####>';
$to = $follow_email;
$subject = "You Have a New Follower!";
$body = "Hello ".$follow."\r\n\r\n".
"".$member." has added you on ####BLANKED####! \r\n".
"You can add them back by clicking the following link. \r\n\r\n".
"http://####BLANKED####/engine/follow.php?follow=".$member_refined." \r\n\r\n".
"(If the link looks odd, DO NOT CLICK IT! We are not responsable for \r\n".
"any damage caused to you, your device, or anything else!) \r\n\r\n".
"Regards,\r\n".
"####BLANKED####\r\n\r\n";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => '####BLANKED####',
'password' => '####BLANKED####'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
}
header('Location: http://####BLANKED####/engine/search.php?search='.$searched);
die;
?>发布于 2014-02-07 04:12:15
您需要UPDATE语句中字符串周围的引号:
mysql_query("UPDATE members SET following='$member_following' WHERE email='$member_email'") or die (mysql_error());如果您检查了来自mysql_query的错误(如我所示),您就会看到这个错误。
您还需要转义数据以防止SQL注入。如果您转换为PDO或mysqli并使用参数化查询,情况会更好。
发布于 2014-02-07 04:18:41
正如其他人所发现的,您遗漏了$member_following的引号
建议添加如下错误检测:
$result = mysql_query($your_sql);
if ( !$result) {
die('Invalid query: ' . mysql_error());
}顺便说一句,保持这样的跟踪成员对数据的一致性没有好处。删除成员时,可能必须扫描所有其他成员以清除$member_following字段,否则该字段可能已删除成员。
一种正常化的方法是为这种多到多的关系提供一个member_follow_member表。我可以看到,您在非常低的级别上使用MySQL (即不使用ORM,甚至不使用PDO ),这样做确实会很痛苦。将PHP与HTML/Javascript混为一谈是另一个糟糕的做法。
https://stackoverflow.com/questions/21619101
复制相似问题