有没有办法将cookies插入数据库?
我需要将用户名与数据库中的值进行匹配,因此我想插入保存在 cookie 中的用户名。该功能非常简单。 cookie 存储正确,我可以回显它。我的插入脚本也可以工作,因为我可以插入其他东西。但由于某种原因我无法插入 cookie 值。
这几乎就是我想做的事情:
$username = $_COOKIE['username'];
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('error');
$query1 = "INSERT INTO Gallery (username) VALUES('$username')";
$data1 = mysqli_query ($dbc, $query1) or die('error1');
mysqli_close($dbc);
我缺少什么吗?我尝试使用会话,但没有运气。
我还使 cookie 在整个域中都可访问。
I need to match up a users name with a value inside a database, so I want to insert the users name that is saved in a cookie. The function is pretty simple. The cookie is stored correctly and I can echo it. My insert script also works cause I can insert other things. But for some reason I cannot insert a cookies value.
This is pretty much what I'm trying to do:
$username = $_COOKIE['username'];
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die('error');
$query1 = "INSERT INTO Gallery (username) VALUES('$username')";
$data1 = mysqli_query ($dbc, $query1) or die('error1');
mysqli_close($dbc);
Is there something I'm missing? I tried using sessions, but no luck.
I also made the cookie accessible throughout the whole domain.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里(至少)存在两个问题。
$user
的变量,但使用名为$username
的变量来尝试将数据插入数据库There are (at least) two problems here.
$user
but use a variable called$username
to try to insert data into the database可能尝试将您的查询更改为此...
Possibly try changing your query to this...