1

I have a problem. I can't save my array data to database, i don't know why. I try some version:

ver1:

$data=$_SESSION['need_save_data'];
$sql = "INSERT INTO session_search_data (`user_id`,`data`,`date`) VALUES ('" . $_SESSION['web_page_user_data']['id'] . "'," . $db_handler->db->quote(json_encode($data)) . ",'" . time() . "')";
$db_handler->db->query($sql);

and it save the database: [] if i echo my query, and run it in mysql console, it working fine:

INSERT INTO session_search_data (`user_id`,`data`,`date`) VALUES ('8','{\"selected_manufacturer_id\":\"504\"}','1442571431')

I try save the database json_encode, the result is similar, it save empty variable.

Also i try to save to file:

$data=$_SESSION['need_save_data'];
$filename = 'session_data/' . $_SESSION['web_page_user_data']['id'] . '.php';
$file = fopen($filename, "w");
fwrite($file, serialize($data));

I try save with json_encode, var_export, serialize, the result is: save empty variable data.

I use PHP 5.4 last version, i think it is configuration problem, because my code works fine two other servers, and my localhost.

6
  • var_dump($_SESSION['need_save_data']); To see if you have something to save. Commented Sep 18, 2015 at 10:32
  • 1
    what is the size/length of your data table. Commented Sep 18, 2015 at 10:33
  • 2
    You are not checking for errors after calling $db_handler->db->query($sql); Do that and it will tell you what is wrong
    – RiggsFolly
    Commented Sep 18, 2015 at 10:35
  • Var dump result: array(1) { ["selected_manufacturer_id"]=> string(3) "504" } my mysql table data type: Mediumtext
    – Josef
    Commented Sep 18, 2015 at 10:44
  • Why are you trying to serialize or json_encode and array of ONE ITEM?? What is the point??
    – RiggsFolly
    Commented Sep 18, 2015 at 10:56

3 Answers 3

0

Even if you are calling $db_handler->db->quote method, it will add quotes to the json encoded data only. You must wrap the data with quotes as well:

$sql = "INSERT INTO session_search_data (`user_id`,`data`,`date`) 
        VALUES ('" . $_SESSION['web_page_user_data']['id'] . "','" . $db_handler->db->quote(json_encode($data)) . "','" . time() . "')";

If this is not the problem then please verify that you are starting the session before trying to access session variables:

<?php
session_start();

Also, make sure you actually have some data inside the $_SESSION['need_save_data'] variable

2
  • I access the session data, var dump result: array(1) { ["selected_manufacturer_id"]=> string(3) "504" } And i tryed save to file, and not working also this
    – Josef
    Commented Sep 18, 2015 at 10:46
  • Try it by wrapping the data with quotes: '" . $db_handler->db->quote(json_encode($data)) . "' Commented Sep 18, 2015 at 10:48
0

make sure you're running in error_reporting(E_ALL), and make sure that query is successfully executed (if its PDO, make PDO run in PDO::ERRMODE->PDO::ERRMODE_EXCEPTION ), and make sure your fwrite succeeed, like

if(strlen(serialize($data))!==fwrite($file, serialize($data) || !fflush($file)){
throw new Exception("failed to write data to disk!");
}

then im sure the problem will become obvious... maybe you have a full harddrive? :p

0

so why you are trying to insert json into database when you can use explode? use prepared statements to avoid sql injection:

$uid = 8;
$data = $_SESSION['need_save_data']; // content: selected_manufacturer_id:504
$date = time();
$sql = "INSERT INTO `session_search_data` SET `user_id`=?, `data`=?, `date`=?";
if ($query = $dbcon->prepare($sql)) {
$query->bind_param('isi', $uid, $data, $date);
$query->execute();
$query->close();
}

so to retrieve the data from database and transform to json you can do:

$contents = explode(':', $data);
$transform = array();
$transform[$contents[0]] = $contents[1];
$to_json = json_encode($transform); // {"selected_manufacturer_id":"504"}

Not the answer you're looking for? Browse other questions tagged or ask your own question.