【问题标题】:PHP issue with classes类的 PHP 问题
【发布时间】:2012-01-18 04:57:02
【问题描述】:

我对以下代码有一点问题:

<?php
class appointments
{
    private $conn;

    function __construct($conn)
    {
        $this->conn = $conn;
    }

    function usercompany($uid)
    {
        return $this->conn->getone("SELECT company FROM signup WHERE UID = '".$uid."'");
    }

    function hastemplate($uid)
    {
        return $this->conn->getone("SELECT COUNT(parentid) FROM appointments_templates WHERE parentid = '$uid'");
    }

    function gettemplate($uid)
    {
        if($this->hastemplate($uid)){
            return $this->conn->getrow("SELECT * FROM appointments_templates WHERE parentid = '" . $uid . "'");
        } else {
            $appointment_template = 'This is a reminder to let you know that you have an appointment on {appointment} at '. $this->usercompany($uid);

            $sql = "INSERT INTO appointments_templates SET parentid = '" . $uid . "', content = '" . $appointment_template . "'";
            $this->conn->execute($sql);

            return $this->gettemplate($uid);
        }
    }  
}
?>

当我自己调用usercompany($uid) 时,我得到一个正确的结果,即用户公司。然而,当我调用gettemplate($uid) 时,新模板被添加到数据库中,但没有usercompany($uid) 的结果。我做错了吗?

编辑:关于 INSERT 语法:http://milov.nl/2836

回顾一下:问题不在于它没有被添加到数据库中,而是被插入为“这是一个提醒,让您知道您在 XYZ 的 {appointment} 有一个约会公司”它被添加为“这是一个提醒,让您知道您在 {appointment} 上有一个约会”

【问题讨论】:

  • 能把使用这个类的代码放上来吗?

标签: php mysql oop class adodb


【解决方案1】:

insert 语句不能这样工作。而不是:

insert into table set column1 = value1, column2 = value2

他们是这样工作的:

insert into table (column1, column2) values (value1, value2)

此外,我没有看到任何转义,所以我希望没有人拥有名称中带有撇号的公司。

【讨论】:

  • 问题不在于它没有被添加到数据库中,而是问题在于它被添加为“这是一个提醒,让您知道您在 {appointment} 上有约会” “这是一个提醒,让您知道您在 XYZ Inc. 的 {appointment} 上有一个约会。”公司名称存储在数据库中时被转义。
【解决方案2】:

您的 insert 查询似乎有错误的语法。你可以在这里参考mysql插入查询的语法http://www.w3schools.com/php/php_mysql_insert.asp

尝试使用这个而不是使用单引号,

$appointment_template = "这是一个提醒,让您知道您在 {appointment} 有一个约会,地点是 "。 $this->usercompany($uid)."";

【讨论】:

  • 问题不在于它没有被添加到数据库中,而是问题在于它被添加为“这是一个提醒,让您知道您在 {appointment} 上有约会” of “这是一个提醒,让您知道您在 XYZ Inc. 的 {appointment} 上有一个约会。”
  • 还有 $appointment_template = "这是一个提醒,让您知道您在 {appointment} 有一个约会,地点是 "。 $this->usercompany($uid)."";不工作。
  • 尝试将 $this->usercompany($uid) 存储在变量中,并查看函数是否返回任何值。我的意思是在 gettemplate 里面。
  • @MeisamMulla $this->usercompany($uid) 的返回值是多少?可能它正在返回一个空字符串...
  • 是用户的公司名称。当我调用 $appointment->usercompany($uid) 时,我得到了正确的结果。
猜你喜欢
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 2010-11-16
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多