【问题标题】:PHP No return from a function in another filePHP没有从另一个文件中的函数返回
【发布时间】:2010-11-24 09:48:45
【问题描述】:

我有一个带有表单的文件,其中包含另一个用于处理该表单的文件。带有表单的文件调用包含文件中的函数将数据写入数据库,然后我从该帖子返回一个 $insert_id,以便我可以在输出中引用它。

例如,当您在页面上填写表格时,数据从单独的文件发送到数据库,然后我想引用它在我调用函数的原始文件中给出的 ID。

文件的 sn-p 格式包含 db 进程文件:

if (isset($_POST['EC_doPost']))
{
    $statusPost = $_POST['EC_statusPost'];

    echo "HERE: " . $this->addEvent($title, $location, $linkout, $attendees, $description, $startDate, $startTime, $endDate, $endTime, $accessLevel, $postID) . "There.";

    $data = array
    (
        'post_content' => $output . "TESTING THIS: " . $event_id,
        'post_title' => $title,
        'post_date' => date('Y-m-d H:i:s'),
        'post_category' => $wpdb->escape($this->blog_post_author),
        'post_status' => $statusPost,
        'post_author' => $wpdb->escape($this->blog_post_author),
    );
}

这里是应该返回 insert_id 的函数(在包含的文件中):

function addEvent($title, $location, $linkout, $attendees, $description, $startDate, $startTime, $endDate, $endTime, $accessLevel, $postID)
{
    $postID = is_null($postID) ? "NULL" : "'$postID'";
    $location = is_null($location) ? "NULL" : "'$location'";
    $description = is_null($description) ? "NULL" : "'$description'";
    $startDate = is_null($startDate) ? "NULL" : "'$startDate'";
    $endDate = is_null($endDate) ? "NULL" : "'$endDate'";
    $linkout = is_null($linkout) ? "NULL" : "'$linkout'";
    $attendees = is_null($attendees) ? "NULL" : "'$attendees'";
    $startTime = is_null($startTime) ? "NULL" : "'$startTime'";
    $accessLevel = is_null($accessLevel) ? "NULL" : "'$accessLevel'";
    $endTime = is_null($endTime) ? "NULL" : "'$endTime'";

    $sql = "INSERT INTO `$this->mainTable` (`id`, `eventTitle`, `eventDescription`, `eventLocation`, `eventLinkout`, `eventAttendees`,`eventStartDate`, `eventStartTime`, `eventEndDate`, `eventEndTime`, `accessLevel`, `postID`) VALUES (NULL , '$title', $description, $location, $linkout, $attendees, $startDate, $startTime, $endDate, $endTime , $accessLevel, $postID);";

    $this->db->query($sql);

    $landingpage = "'http://www.google.com'";
    $status = "'1'";
    $title = "'".$title."'";
    $addedon = "'".date("Y-m-d")."'";

    $sql = "INSERT INTO `$this->eventrTable` (`name`, `description`, `event_date`, `maximum_attendees`, `added_on`, `status`, `landing_page`) VALUES ($title, $description, $startDate, $attendees, $addedon, $status, $landingpage);"; 

    $this->db->query($sql);
    $event_id = $this->db->insert_id;

    return $event_id;
}

我想做的是让两个 Wordpress 插件一起工作,但目前还没有那么顺利。如果我使用函数 echo 变量制作包含的文件,它可以工作,但是我无法让它在原始文件中工作......

我确定这很愚蠢,但我很难过。

【问题讨论】:

  • @zombat 我尝试了您提供的修订版,删除了 $,但这导致了语法错误。也许我忽略了什么?
  • Oi,将 SQL 查询连接在一起。您可以编写 WP 插件,而无需像 WP 开发人员那样编写代码。使用准备好的语句!
  • @Lucas_Oman 我不确定你的意思。我主要是一名设计师,比他应该更频繁地涉足代码......
  • 固定代码语法以提高可读性
  • @KeepingYouAwake - 哦,我没有提供您的代码的修订版。我刚刚编辑了问题,因此代码格式将在接近尾声时正确显示。

标签: php oop wordpress function return


【解决方案1】:

addEvent() 函数没有返回 $event_id;

你应该有这样的东西:

function addEvent(/*...*/)
{
    // ...

    $sql = "INSERT INTO `$this->eventrTable` ("
      ."`name`, `description`, `event_date`, `maximum_attendees`, `added_on`, `status`, `landing_page`) "
      ."VALUES ($title, $description, $startDate, $attendees, $addedon, $status, $landingpage);";

    $this->db->query($sql);
    $event_id = $this->db->insert_id;

    return $event_id;
}

尝试执行以下操作:

var_dump($this->db->query($sql));
var_dump($event_id);

并发布输出。

【讨论】:

  • 这就是我所拥有的。它不起作用,我不明白为什么......见上文。抱歉,我刚刚编辑了代码,删除了一些干扰代码显示的 cmets。
  • 那我不明白你到底是什么问题,也许你把整个文件贴出来我很容易发现问题。
  • 可能是您的查询有问题,请检查我的答案并发布输出。
  • 输出分别如下所示: var_dump($this->db->query($sql)); var_dump($event_id) int(1) int(23)
  • @KeepingYouAwake: echo "HERE:" 的输出是什么。 $this->addEvent(...) 。 “那里。”;?
猜你喜欢
  • 2010-09-28
  • 1970-01-01
  • 2012-03-03
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
  • 2020-09-03
相关资源
最近更新 更多