Blog

Snippet of How to Insert Data from a Form Field into a MySQL Database Using PHP
Posted on June 19, 2015 in MySQL, PHP by Matt Jennings

PHP Below on a Could be Found on a process.php File that Processes a Form

    /*
    Set 'name' and 'quote' input/text area session variables
    if they don't exist
    */
    if(!isset($_SESSION['name']) && !isset($_SESSION['quote']))
    {

        $_SESSION['name'] = $_POST['name'];
        $_SESSION['quote'] = $_POST['quote'];

        // Save a MySQL query as a $query variable using
        // data from form fields
        $insert_query = "INSERT INTO quotes(name, quote, created_at, updated_at) VALUES ({$_SESSION['name']}, {$_SESSION['name']}, NOW(), NOW())";

        // Pass the $query variable into a called function
        // and save it as a $execute_query variable
        $execute_insert_query = run_mysql_query($insert_query);

        header('Location: main.php');

    }

Leave a Reply

To Top ↑