1

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';
    }
}
User014019
  • 1,215
  • 8
  • 34
  • 66
  • run the query on database editor and check the error or warning if any – Mayank Pandeyz Oct 12 '16 at 04:04
  • 1
    Where is $conn coming from? You may be able to turn up a debug level to see what error occurs on ELSE. e.g. $conn->getError(); or similar. You may also want to use ini_set('display_errors', 1); error_reporting(E_ALL) for development... – JI-Web Oct 12 '16 at 04:09
  • which one is the date field in above code... – Veshraj Joshi Oct 12 '16 at 05:03
  • @VeshrajJoshi, i didn't put the date field yet since I don't know how to insert in when the other values are inserted – User014019 Oct 12 '16 at 05:14
  • @JI-Web $conn is the db connection only. I already use the `ini_set('display_errors', 1); ` but the error displayed only is `Error` – User014019 Oct 12 '16 at 05:16
  • 1
    you can add field name and values, otherwise set default value to the column in database – Veshraj Joshi Oct 12 '16 at 05:24

1 Answers1

1

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.

Community
  • 1
  • 1
JazZ
  • 4,469
  • 2
  • 20
  • 40
  • but how to save the date inserted automatically? I mean, I want to save the date inserted when the values are inserted – User014019 Oct 12 '16 at 05:13
  • @User014019 I edited my answer to insert the current datetime (actually, the datetime when the values are insert). Also, your `date_inserted` MySQL field has to be "DATETIME" type. – JazZ Oct 12 '16 at 05:26
  • it's working now. thanks! – User014019 Oct 12 '16 at 05:41