User014019 User014019 - 1 month ago 8
MySQL Question

Values not saving in the MYSQL using PHP

I'm trying to save the utm parameters and other values in mysql using PHP, but the values are not savind in the database.

I already echo each variables and the values are present. What's wrong with the query? Why the values are not saving in the database?

And how to insert the date? I mean, I want to insert/save the date when the values are inserted.

if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != ""){
$customer_fname = isset($_POST['customer_fname']) ? $_POST['customer_fname'] : '';
$customer_sname = isset($_POST['customer_sname']) ? $_POST['customer_sname'] : '';
$customer_email = isset($_POST['customer_email']) ? $_POST['customer_email'] : '';
$customer_mobile = isset($_POST['customer_mobile']) ? $_POST['customer_mobile'] : '';
$utm_source = isset($_GET['utm_source']) ? $_GET['utm_source'] : '';
$utm_medium = isset($_GET['utm_medium']) ? $_GET['utm_medium'] : '';
$utm_campaign = isset($_GET['utm_campaign']) ? $_GET['utm_campaign'] : '';
$utm_content = isset($_GET['utm_content']) ? $_GET['utm_content'] : '';
$referer_url = $_SERVER['HTTP_REFERER'];
$base_url = preg_replace('/\?.*/', '', $_SERVER['HTTP_REFERER']);

$sql = "INSERT INTO utm_param(customer_fname,customer_sname,
customer_email,customer_mobile,
utm_source,utm_medium,
utm_campaign,utm_content,
referer_url,base_url,date_inserted)
VALUES('".$conn->real_escape_string($customer_fname)."',
'".$conn->real_escape_string($customer_sname)."',
'".$conn->real_escape_string($customer_email)."',
'".$conn->real_escape_string($customer_mobile)."',
'".$conn->real_escape_string($utm_source)."',
'".$conn->real_escape_string($utm_medium)."',
'".$conn->real_escape_string($utm_campaign)."',
'".$conn->real_escape_string($utm_content)."',
'".$conn->real_escape_string($referer_url)."',
'".$conn->real_escape_string($base_url)."')";

$result = $conn->query($sql);

if($result){
echo "row inserted\n";
} else {
echo '<br/>aaa';
}
}

Answer

In the query, you miss the date_inserted value. If it has to be "NOT NULL", the query will fail.

To insert a date in DB, I suggest you see this link : PHP date() format when inserting into datetime in MySQL.

So following the link above, to save the date of when the values where inserted, you'll have to do something like this :

// set the value
$date_inserted = date("Y-m-d H:i:s");

//insert the values
$sql = "INSERT INTO utm_param(
    customer_fname,
    customer_sname,
    customer_email,
    customer_mobile,
    utm_source,
    utm_medium,
    utm_campaign,
    utm_content,
    referer_url,
    base_url,
    date_inserted
) VALUES(
    '".$conn->real_escape_string($customer_fname)."',
    '".$conn->real_escape_string($customer_sname)."',
    '".$conn->real_escape_string($customer_email)."',
    '".$conn->real_escape_string($customer_mobile)."',
    '".$conn->real_escape_string($utm_source)."',
    '".$conn->real_escape_string($utm_medium)."',
    '".$conn->real_escape_string($utm_campaign)."',
    '".$conn->real_escape_string($utm_content)."',
    '".$conn->real_escape_string($referer_url)."',
    '".$conn->real_escape_string($base_url)."',
    '".$date_inserted."'
);"; 

Hope it helps.