SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Mentor silver trophybronze trophy
    alabamaseo's Avatar
    Join Date
    Dec 2012
    Location
    Birmingham, AL
    Posts
    203
    Mentioned
    25 Post(s)
    Tagged
    0 Thread(s)

    Trying to Save Data from a form in Database

    Hello everyone,

    I have been trying to create a form that saves the only field (email) in a localized database. When I submit my information through the site, the entire "newsletter.php" process is being completed, but there are no new rows in the table I created. Can someone please let me know if there is something wrong with the code?

    PHP Code:
    <?php

    define
    ('DB_NAME''xxx');
    define('DB_USER''xxx');
    define('DB_PASS''xxx');


    function 
    log_for_grid($data)
    {
        
    $db = new mysqli('localhost'DB_USERDB_PASSDB_NAME);
        if ( ! 
    mysqli_connect_errno())
        {
            
    $sql '';
            foreach (
    $data as $k => $v)
            {
                
    $sql .= sprintf("`%s` = '%s', "$k$db->real_escape_string($v));
            }
            
    $sql .= "date = NOW() ";

            
    $db->query("INSERT INTO emails SET " $sql);

            
    $db->close();
        }
    }

    function 
    add_if_filled($data)
    {
        
    $out '';
        foreach (
    $data as $k => $v)
        {
            
    $out .= empty($v) ? '' "$k$v\n";
        }
        return 
    $out;
    }

    PHP Code:
    <?php
         
    require('./database.inc.php');

        function 
    clean_string($string) {
          
    $bad = array("content-type","bcc:","to:","cc:","href");
          return 
    str_replace($bad,"",$string);
        }


          
        
    $email_from $_POST['email']; // required
       


    function db_log($data)
    {
        
    $db = new mysqli('localhost'DB_USERDB_PASSDB_NAME);
        if ( ! 
    mysqli_connect_errno())
        {
            
    $data['email'] = $db->real_escape_string($data['email']);

            
    $sql "INSERT INTO `emails` (date, email) VALUES ";
            
    $db->query($sql);
            
    $db->close();
        }
    }

    ?>
    Levy's Fine Jewelry- buyers and sellers of modern, antique, and estate jewelry

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,068
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    What errors if any is MySQL returning?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    San Sebastian, Spain
    Posts
    93
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Looking at your SQL code, you have not completed the INSERT statement. You have

    $sql = "INSERT INTO `emails` (date, email) VALUES ";
    $db->query($sql);

    This will fail with a syntax error.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •