Example #1
0
<?php

require_once '../api_builder_includes/class.Database.inc.php';
Database::init_connection("localhost", "organization", "users", "username", "secret_password");
// Array containing the row to change. The only required values are the id and the column being changed.
// All other key => value pairs are ignored but are present here because often rows are updated in batch
// after being returned in 2D array fashion from Database::get_all_results();
$user = array("id" => 2, "first_name" => "Salvester", "last_name" => "Rinehart", "email" => "*****@*****.**", "phone_number" => "8042557684", "city" => "Richmond", "state" => "VA", "bio" => "Total badass.");
//sanitize user input
$user_cleaned = Database::clean($user);
//the 3rd parameter specifies this is an update statement by selecting which column from in the row to update
if (Database::execute_from_assoc($user_cleaned, Database::$table, "phone_number")) {
    echo $user['first_name'] . "'s phone number was changed to " . $user['phone_number'];
}
require_once '../api_builder_includes/class.Database.inc.php';
var_dump($_POST);
//if POST is present...
if (isset($_POST) && !empty($_POST)) {
    // Do any neccissary validation here. You can use something like https://github.com/ASoares/PHP-Form-Validation
    // if you are not going to validate input, which you absolutely should if users are submitting it, then at least
    // make sure the correct values are present
    if (isset($_POST['first_name']) && !empty($_POST['first_name']) && isset($_POST['last_name']) && !empty($_POST['last_name']) && isset($_POST['email']) && !empty($_POST['email']) && isset($_POST['phone_number']) && !empty($_POST['phone_number']) && isset($_POST['city']) && !empty($_POST['city']) && isset($_POST['state']) && !empty($_POST['state']) && isset($_POST['bio']) && !empty($_POST['bio'])) {
        // Open the database connection. This is what happens inside of the API class constructor
        // but if this page is simply for submitting data to the database you can just call this method
        Database::init_connection("localhost", "organization", "users", "username", "secret_password");
        // Sanitize the array so that it can be safely inserted into the database.
        // This method uses MySQLi real escape string and htmlspecialchars encoding.
        $post_array = Database::clean($_POST);
        //submit the data to your table.
        if (Database::execute_from_assoc($post_array, Database::$table)) {
            echo "The data was submitted to the database";
        } else {
            echo "There was an error submitting the data to the database";
        }
    } else {
        echo "One or more of the required values is missing from the POST";
    }
} else {
    echo "Nothing was added to the database because the http request has no POST values";
}
?>

<form method="post" action="">
    <label for="first-name">First Name</label>
    <input type="text" id="first-name" name="first_name"/>