Jump to content

mysql_real_escape_string w/ session data


seaweed

Recommended Posts

I'm using the POST method to pass data to a script, and then putting the data into a $_SESSION array. Ultimately the data will be put into an SQL database, so i want to cleanse it with mysql_real_escape_string, but when I use mysql_real_escape_string and then put the data into the $_SESSION array, all of the variables are empty.

 

For example, this works:

 

$prod_id = $_POST['prod_id'];
$prod_qty = $_POST['prod_qty'];
$prod_size = $_POST['prod_size'];
$prod_color = $_POST['prod_color']; 

$_SESSION['CART']['ITEMS'][] = array(
   'prod_id' => $prod_id,
   'prod_qty' => $prod_qty,
   'prod_size' => $prod_size,
   'prod_color' => $prod_color
);

 

 

This does not:

 

$prod_id = mysql_real_escape_string($_POST['prod_id']);
$prod_qty = mysql_real_escape_string($_POST['prod_qty']);
$prod_size = mysql_real_escape_string($_POST['prod_size']);
$prod_color = mysql_real_escape_string($_POST['prod_color']); 

$_SESSION['CART']['ITEMS'][] = array(
   'prod_id' => $prod_id,
   'prod_qty' => $prod_qty,
   'prod_size' => $prod_size,
   'prod_color' => $prod_color
);

 

 

Is there a reason why?

 

Is there a better way to clean the data before I stuff it in the session?

 

Link to comment
https://forums.phpfreaks.com/topic/156747-mysql_real_escape_string-w-session-data/
Share on other sites

Use mysql_real_escape_string right before INSERTing the data into database. There's no need to escape it before, and as you can see you run into all kinds of problems with that. Not to mention you risk double (or multiple) escaping same data.

 

mysql_connect has to be called in each script that queries database.

 

mysql_pconnect does not work as most people expect it to. Read the comments on the manual page.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.