Created
March 14, 2013 21:21
-
-
Save amwelles/5165395 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// get the request | |
$data = $_REQUEST['payload']; | |
// sanitize the data | |
$unescaped_data = stripslashes($data); | |
// set up our object | |
$person = json_decode($unescaped_data); | |
// assign variables | |
$id = $person->signup->nationbuilder_id; // used as a unique identifier | |
$first_name = $person->signup->first_name; | |
$last_name = $person->signup->last_name; | |
$mobile_phone = $person->signup->mobile_number; | |
$work_phone = $person->signup->work_phone_number; | |
$other_phone = $person->signup->phone_number; | |
$email_1 = $person->signup->email1; | |
$email_2 = $person->signup->email2; | |
$email_3 = $person->signup->email3; | |
// connect to our DB | |
$connect = mysql_connect("HOST", "USER", "DB PASSWORD"); | |
mysql_select_db("DATABASE_NAME", $connect); | |
// let's create this table if it doesn't exist | |
$sqltable = "CREATE TABLE IF NOT EXISTS webhook_backup | |
( | |
`first_name` varchar(80), | |
`last_name` varchar(80), | |
`nb_id` int, | |
`mobile_phone` varchar(20), | |
`work_phone` varchar(20), | |
`other_phone` varchar(20), | |
`email_1` varchar(255), | |
`email_2` varchar(255), | |
`email_3` varchar(255) | |
)"; | |
// does this user exist in the DB already? | |
$query = "SELECT `nb_id` FROM `webhook_backup` WHERE `nb_id` = '". $id ."'"; | |
$num_rows = mysql_num_rows($query); | |
// update if this user already exists | |
if ($num_rows > 0) { | |
$update = "UPDATE `webhook_backup` SET | |
`first_name` = '". $first_name ."' | |
`last_name` = '". $last_name ."' | |
`mobile_phone` = '". $mobile_phone. "' | |
`work_phone` = '". $work_phone. "' | |
`other_phone` = '". $other_phone. "' | |
`email_1` = '". $email_1 ."' | |
`email_2` = '". $email_2 ."' | |
`email_3` = '". $email_3 ."' | |
WHERE `nb_id` = '". $id ."' | |
LIMIT 1"; | |
mysql_query($update, $connect); | |
} | |
// add a new row if this user does not yet exist | |
if ($num_rows == 0) { | |
$insert = "INSERT INTO `webhook_backup` | |
(`first_name`, `last_name`, `mobile_phone`, `work_phone`, `other_phone`, | |
`email_1`, `email_2`, `email_3`, `nb_id`) | |
VALUES ('". $first_name ."','". $last_name ."','". $mobile_phone ."','". $work_phone ."', | |
'". $other_phone ."','". $email_1 ."','". $email_2 ."','". $email_3 ."','". $id ."')"; | |
mysql_query($insert, $connect); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment