Help needed with webhook/php inserting data in MySQL table

I am trying to post the analog output from A0 to my MySQL database every couple of minutes. I am using https so I figured a webhook would be the best way to go. I was able to get a webhook set-up but I am stuck on how to read this in with my php script and insert it in my database.

First I wanted to make sure the webhook posted the correct data, so I set another webhook up to send it to http://requestb.in/. Below is the post I received on the request site:

{“event”:“testpost4”,“data”:“801”,“published_at”:“2015-08-25T03:18:03.538Z”,“coreid”:“XXXXXXXXXXXXXXXXXXXX”,“name”:“testpost4”,“value”:“801”,“source”:""

So I think I am receiving the correct values for coreid, name, and value (I purposely Xed out the core id). So this tells me that the photon code and webhook are working properly (Correct me if I’m wrong).

So my issue arises when I try to post this data to my url. I have limited experience with php but I have written a few mobile apps that post data and below is a simplified (no security) version of what I would write for those.

<?php 

//load and connect to MySQL database stuff 

require("config.inc.php"); 

if (!empty($_POST)) { 

//initial query 

$query = "INSERT INTO test_post ( name, coreid, value ) VALUES ( :name, :coreid, :value ) "; 

//Update query 

$query_params = array( 

    ':name' => $_POST['name'], 

    ':coreid' => $_POST['coreid'], 

    ':value' => $_POST['value'] 

); 

//execute query 

try { 

    $stmt   = $db->prepare($query); 

    $result = $stmt->execute($query_params); 

} 

catch (PDOException $ex) { 

    $response["success"] = 0; 

    $response["message"] = "Database Error."; 

    die(json_encode($response)); 

} 

$response["success"] = 1; 

$response["message"] = "Entry Added"; 

echo json_encode($response); 

} else { 

?> 

    <h1>Post Values</h1>  

    <form action="testpost.php" method="post">  

        Name:<br />  

        <input type="text" name="name" placeholder="name" />  

        <br /><br />  

        Value:<br />  

        <input type="text" name="value" placeholder="value" />  

        <br /><br /> 

        Source:<br />  

        <input type="text" name="coreid" placeholder="coreid" />  

        <br /><br /> 

        <input type="submit" value="Submit Values" />  

    </form>  

<?php 

} 

?> 

However when I try this code nothing happens. As you can see from the code I wrote a small web interface to test inserting values into the database and that works fine, so the issue has to be between the webhook and the php script.

I’m sure it is something simple but as I said I have limited experince and have not been able to find any examples or tutorials covering this. If there is one please point me too it. Otherwise any help would be appreciated. Thanks!

I’m no expert in PHP, but it looks like you’re not closing your initial if statements (the one that checks if the POST variables are empty).

Do you want the form to show up if there are no POST variable received? If so, shouldn’t it be something like this?

<?php

//load and connect to MySQL database stuff 

require("config.inc.php");

if (!empty($_POST)) {
    
    //initial query 
    
    $query = "INSERT INTO test_post ( name, coreid, value ) VALUES ( :name, :coreid, :value ) ";
    
    //Update query 
    
    $query_params = array(
        
        ':name' => $_POST['name'],
        
        ':coreid' => $_POST['coreid'],
        
        ':value' => $_POST['value']
        
    );
    
    //execute query 
    
    try {
        
        $stmt = $db->prepare($query);
        
        $result = $stmt->execute($query_params);
        
    }
    
    catch (PDOException $ex) {
        
        $response["success"] = 0;
        
        $response["message"] = "Database Error.";
        
        die(json_encode($response));
        
    }
    
    $response["success"] = 1;
    
    $response["message"] = "Entry Added";
    
    echo json_encode($response);
    
} else {
    
    echo ' <h1>Post Values</h1>  
    
        <form action="testpost.php" method="post">  
    
            Name:<br />  
    
            <input type="text" name="name" placeholder="name" />  
    
            <br /><br />  
    
            Value:<br />  
    
            <input type="text" name="value" placeholder="value" />  
    
            <br /><br /> 
    
            Source:<br />  
    
            <input type="text" name="coreid" placeholder="coreid" />  
    
            <br /><br /> 
    
            <input type="submit" value="Submit Values" />
    
        </form> ';
}
?>

Just my thoughts. Hopefully it helps!
Everything else in your code looks correct. If it still doesn’t work for you, I’d recommend adding in some other debug functions (perhaps a mail function that alerts you as to how far into the function you were able get)

I always found the easiest way to find what was happening was to write to a file. similar to debugging using serial, or returning things to the webhook.. my computer crashed last week so i cant share the whole code...
have a look at this thread..

Thanks for the help I will try implementing some of these suggestions tonight.

1 Like