简体   繁体   English

将PHP变量插入mySQL表

[英]Inserting PHP variable into mySQL table

I have a booking system, in which once a user books a flight, their details are sent to a mySQL table called OABS_customers . 我有一个预订系统,在该系统中,一旦用户预订了航班,他们的详细信息就会发送到名为OABS_customers的mySQL表中。

It's structure is the following: 它的结构如下:

Name         Type(Length)    Null     Default

name         varchar(60)     No       noname
address1     varchar(60)     No       noaddress
address2     varchar(60)     No       noaddress2
county       varchar(60)     No       nocounty
postcode     varchar(32)     No       nopostcode
customer     tinyint(4)      No       
seats        varchar(11)     No       0
number       tinyint(4)      No       0
class        varchar(11)     No       noclass
username     varchar(60)     No       nousername

Now, my script adds all of the details with no problem - except for one column - the username column. 现在,我的脚本可以毫无问题地添加所有详细信息-除了一列- username名列。 I'm using this code: $query = "INSERT INTO OABS_customers (username) VALUES ('$user')"; 我正在使用以下代码: $query = "INSERT INTO OABS_customers (username) VALUES ('$user')"; (provided kindly by eggyal) to take the logged in users name (which is stored in the cookie ID_my_site and $user = $_COOKIE['ID_my_site'] - and I have tested that and it does contain the username of the logged in user) and add it into the username table, however this doesn't seem to work, and it instead displays the default value of nousername . (由eggyal友善提供)获取登录的用户名(存储在Cookie ID_my_site和$user = $_COOKIE['ID_my_site'] -我已经测试过,它确实包含登录用户的用户名)并将其添加到用户名表中,但这似乎不起作用,而是显示默认值nousername

If anyone could help amend this issue I would greatly appreciate it. 如果有人可以帮助修改此问题,我将不胜感激。

You need to modify you login mechanism to track the user ID for the session. 您需要修改登录机制以跟踪会话的用户ID。 Store the ID in memory or in a cookie. 将ID存储在内存或cookie中。

Once you can store the user id now you can save it to the DB. 现在可以存储用户ID了,您可以将其保存到数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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