简体   繁体   中英

Can't save php variable to mysql database or to file

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.

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

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

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"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM