假设我有一个包含两个表的数据库:
id
name
email
password
另一个拥有他们存储在数据库中的品牌:
id
brand_id
brand_quantity
如何确保如果'Alex'登录到他的帐户,他将能够访问他的信息,编辑,添加,删除而不是其他人的? 我写的PDO脚本可以修改,编辑,删除所有信息,无论是Alex的信息还是John的信息。我如何超越它?
脚本:
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
}
if (isset($_POST['sub']) == 1) {
$id2 = $_POST['id2'];
$qty2 = $_POST['qty2'];
$stmt = $conn->prepare("SELECT brand_qty FROM brands WHERE brand_id = :id2");
$stmt->bindParam(':id2', $id2);
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
$s = $v - $qty2;
if ($s >= 3) {
echo "<script>alert('Almost out of stock!');</script>";
}
$sth = "UPDATE brands SET brand_qty='$s' WHERE brand_id = '$id2'";
$conn->exec($sth);
echo "<script>alert('New record successfully updated.')</script>";
}
表:
成员表图像
他们的品牌和数量表
答案 0 :(得分:0)
首先定义表之间的关系。例如,在品牌表中添加userid
。
所以你的品牌表将是:
id
brand_id //i dont know why you have used this
user_id
brand_quantity
然后使用以下查询更改您的查询:
$userid = YOUR_SESSION_ID;
"SELECT brand_qty FROM brands WHERE brand_id = :id2 and user_id = :userid";
"UPDATE brands SET brand_qty='$s' WHERE brand_id = '$id2' and user_id='$userid'";
SIDE NOTE :要详细了解两个表格之间的关系,您可以浏览此链接:http://www.w3schools.com/sql/sql_foreignkey.asp