将我的 cookie 数据保存到 MySQL 数据库?

2021-12-21 00:00:00 offline database cookies php javascript

我在做一项调查,我是离线服务器,所以我将用户信息保存在 cookie 中,我有这样的用户信息

I was doing a survey I was offline server so I save users information in cookies, I have users Information like this

Fname:Ali,Lname:Kheiri,title:Director,org:Magiclamp,email:alikheiri@gmail.comFname:Ali,Lname:Kheiri,title:Director,org:Magiclamp,email:alikheiri@gmail.com

它保存在cookies中,但现在我在线,所以我想将此cookies数据保存在php中的mysql数据库中,为此我编写了javascript代码,但它不起作用

it is save in cookies, but now i am online, so i want to save this cookies data in mysql database in php, javascript for this I write this code but its not working

    mysql_connect("localhost","root","");//database connection05
    mysql_select_db("o2measure");
    if($_POST['submit']){
        $date = date('Y,m,d');
       $time = date("i:h:s",time() + $time_a);
       echo $session_id;
       $ip=$_SERVER['REMOTE_ADDR'];
        foreach($_COOKIE as $name => $cookie){
    // you can check any conditions based on cookie name $name
    $sql="INSERT INTO user_data (first_name, last_name, job_title, organisation, email, phone_number, ip_address, date, time)
     VALUES(" . mysql_escape_string($cookie) . ")";
        $result=mysql_query($sql); 

}

推荐答案

你应该将 cookie 值作为一个字符串拆分它并单独使用每个条件而不是使用插入查询,因为你写的查询不正确你说插入 user_data名字 姓氏 职位名称...但您只给他 1 个值 $cookie 但 sql 不知道 $cookie 包含所有内容,因此您应该首先根据 ',' slpit cookie 值

you should get the cookie value as a string split it and have every criteria alone than use the insert query because the query you wrote is incorrect you saying insert into user_data the first name the last name the job title... but you are only giving him 1 value the $cookie but the sql don't know that the $cookie contain everything so you should first slpit the cookie value based on the ','

相关文章