views:

277

answers:

1

I have one file with a form, that includes another to process that form. The file with the form calls a function in the included file to write data to the database, and then I return an $insert_id from that post so I can reference it in the output.

For example, when you fill out the form on the page, the data is sent to the db from a separate file, then I want to reference the ID it was given in the original file that I called the function from.

A snippet of the file with the form that includes the db process file:

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),
    );
}

And here is s the function (in the included file) that should return the 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;
}

What I am trying to do is make two Wordpress plugins work together, but it's not going so well just yet. If I make the included file with the function echo the variable, it works, but then I can't get it to work in the original file...

I'm sure it's something dumb, but I'm stumped.

A: 

The addEvent() function is not returning the $event_id;

You should have something like this:

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;
}

Try doing the following:

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

And post the output.

Alix Axel
That's what I do have. It's not working and I don't understand why... See above. Sorry, I did just edit the code to remove some comments that were disrupting the code display here.
Then I don't understand exactly what your problem is, maybe if you posted the whole file I could easily spot the problem.
Alix Axel
Maybe it's a problem with your query, check my answer and post the output.
Alix Axel
outputs shown respectively below:var_dump($this->db->query($sql));var_dump($event_id)int(1)int(23)
@KeepingYouAwake: what is the output for echo "HERE: " . $this->addEvent(...) . "There.";?
Alix Axel
Also, is your first code snippet inside a class?
Alix Axel
@eyze The output for that line is "HERE: There.". It does complete the function of adding the entry to the database but doesn't return anything from that function, even if I set the return to a defined test string like return "CHEESE";.Yes, it is inside a class called "EC_Management", then within the function "display()" of that class.
Weird, right? I am seeing the results of the echo, except for the output of the function, but the function does run. If I echo from the function, it outputs to the screen, but if I return a var from the function, no good.
Yep, I'm totally lost then.
Alix Axel
Bummer... Would it help to email you the files, or is there perhaps a way to bring more attention to this question? I'm stuck, but in a way that seems like something odd is going wrong.
Try editing your question to provide more detail about the problem and other files, if you still don't get any solutions you can start up a bounty (but I think you need to have 100 reputation points to open one).
Alix Axel