I'm making a simple webshop for my university project that uses two tables from database (users and items). I try to insert into the database some information about the specific item. The last column is the id of the logged user. I make query that receiving from 'users' table the id of the actually logged user. When I use this variable ($lastlogin) printing via echo it shows a correct value. Unfortunately when I try to insert the id to the table 'items' with insert query, there goes 0 instead of the correct value (eg. 5). Does anyone know how to fix my problem? I will be grateful.
我正在为我的大学项目创建一个简单的网上商店,它使用数据库中的两个表(用户和项目)。我尝试在数据库中插入一些有关特定项目的信息。最后一列是已记录用户的ID。我查询从'users'表接收实际登录用户的id。当我使用此变量($ lastlogin)通过echo打印时,它显示正确的值。不幸的是,当我尝试使用insert查询将id插入表'items'时,会出现0而不是正确的值(例如5)。有谁知道如何解决我的问题?我会很感激。
$idlogin = "SELECT id FROM users WHERE login=:login";
$query = $db->prepare($idlogin);
$query->bindParam(":login", $_SESSION['login']);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
$lastlogin = $row["id"];
$sql = "INSERT INTO `items` VALUES (NULL, :name, :description, :cust_id)";
$params = [ ":name" => trim($_POST["name"]),
":description" => trim($_POST["description"]),
":cust_id" => '$lastlogin'];
$query = $db->prepare($sql);
$query->execute($params);
2 个解决方案
#1
2
You don't have to put $lastlogin
between quotes in your params. If you do, mysql will see this as a string with the value "$lastlogin"
. Mysql won't be able to parse this to an integer and add the value 0
instead.
您不必将$ lastlogin放在params中的引号之间。如果这样做,mysql会将其视为一个值为“$ lastlogin”的字符串。 Mysql将无法将此解析为整数并添加值0。
$params = [
":name" => trim($_POST["name"]),
":description" => trim($_POST["description"]),
":cust_id" => $lastlogin
];
#2
0
Ok, thanks for everyone who tried to help me
好的,谢谢所有试图帮助我的人
I found a clue in Michael Berkowski response at this thread $lastlogin was initialized only the first time, so I put it into $_SESSION['lastlogin'] and call it in sql query.
我在Michael Berkowski的回复中发现了一个线索$ lastlogin只是第一次初始化,所以我将它放入$ _SESSION ['lastlogin']并在sql查询中调用它。
$idlogin = "SELECT id FROM users WHERE login=:login";
$query = $db->prepare($idlogin);
$query->bindParam(":login", $_SESSION['login']);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
$lastlogin = (int)$row["id"];
$_SESSION['lastlogin'] = $lastlogin;
$sql = "INSERT INTO items VALUES (NULL, :name, :description, :cust_id)";
$params = [
":name" => trim($_POST["name"]),
":description" => trim($_POST["description"]),
":cust_id" => $_SESSION['lastlogin']
];
$query = $db->prepare($sql);
$query->execute($params);
Now everything works great. Thanks again for your time.
现在一切都很好。感谢你的宝贵时间。
#1
2
You don't have to put $lastlogin
between quotes in your params. If you do, mysql will see this as a string with the value "$lastlogin"
. Mysql won't be able to parse this to an integer and add the value 0
instead.
您不必将$ lastlogin放在params中的引号之间。如果这样做,mysql会将其视为一个值为“$ lastlogin”的字符串。 Mysql将无法将此解析为整数并添加值0。
$params = [
":name" => trim($_POST["name"]),
":description" => trim($_POST["description"]),
":cust_id" => $lastlogin
];
#2
0
Ok, thanks for everyone who tried to help me
好的,谢谢所有试图帮助我的人
I found a clue in Michael Berkowski response at this thread $lastlogin was initialized only the first time, so I put it into $_SESSION['lastlogin'] and call it in sql query.
我在Michael Berkowski的回复中发现了一个线索$ lastlogin只是第一次初始化,所以我将它放入$ _SESSION ['lastlogin']并在sql查询中调用它。
$idlogin = "SELECT id FROM users WHERE login=:login";
$query = $db->prepare($idlogin);
$query->bindParam(":login", $_SESSION['login']);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
$lastlogin = (int)$row["id"];
$_SESSION['lastlogin'] = $lastlogin;
$sql = "INSERT INTO items VALUES (NULL, :name, :description, :cust_id)";
$params = [
":name" => trim($_POST["name"]),
":description" => trim($_POST["description"]),
":cust_id" => $_SESSION['lastlogin']
];
$query = $db->prepare($sql);
$query->execute($params);
Now everything works great. Thanks again for your time.
现在一切都很好。感谢你的宝贵时间。