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

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


function log_for_grid($data)
{
	$db = new mysqli('localhost', DB_USER, DB_PASS, DB_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\
";
	}
	return $out;
}



<?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_USER, DB_PASS, DB_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();
	}
}

?>

What errors if any is MySQL returning?

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.