在php中对多个值使用一个键
我正在在php中对多个值使用一个键,php,Php,我正在php中开发一个webapplication,其中将有一个名为“我的产品”的区域。在该区域,您可以看到列出的产品。让我们假设你放置了一辆汽车出售。您将有类似于: 型号:R8 颜色:黄色 品牌:奥迪 类型:柴油机 价格:90000 CarID:1 我的问题:如何选择carid,以便将carid作为键,并将其他值(如型号、颜色等)作为值? 我正在使用mysql存储列表SQL表: +---------+-----------------------------------------
php
中开发一个webapplication,其中将有一个名为“我的产品”的区域。在该区域,您可以看到列出的产品。让我们假设你放置了一辆汽车出售。您将有类似于:
- 型号:R8
- 颜色:黄色
- 品牌:奥迪
- 类型:柴油机
- 价格:90000
- CarID:1
mysql
存储列表<代码>SQL表:
+---------+------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------------------------------------------+------+-----+---------+----------------+
| carid | int(11) | NO | MUL | NULL | auto_increment |
| brand | enum('Alfa Romeo','Aston Martin','Audi') | NO | | NULL | |
| color | varchar(20) | NO | | NULL | |
| type | enum('gasoline','diesel','eletric') | YES | | NULL | |
| price | mediumint(8) unsigned | YES | | NULL | |
| mileage | mediumint(8) unsigned | YES | | NULL | |
| model | text | YES | | NULL | |
| year | year(4) | YES | | NULL | |
| user | varchar(30) | YES | | NULL | |
+---------+------------------------------------------+------+-----+---------+----------------+
我的伪代码是这样的:
array\u combine
到foreach-inside-foreach
以来,我尝试了一些代码。
这就是我到目前为止的代码:
$qry_id = "select carid from cars where user='$login'";
if ($car_id = mysqli_query($link, $qry_id)){
while ($row = mysqli_fetch_assoc($car_id)){
//It will fetch the iD of the user in the DB
$user_id = $row["carid"];
$qry_model = "select model from cars where carid='$user_id'";
if($model_obj = mysqli_query($link, $qry_model)){
while ($row_2 = mysqli_fetch_assoc($model_obj)){
$model = $row_2['model'];
$final_array = array($user_id => $model);
}
}
}
}
我不知道这是否是正确的方法,因为这是我正在构建的第一个Web应用程序。
为了简单起见,我只是在代码中使用模型值
您可以执行以下操作
请注意,此查询不安全,并且倾向于SQL注入,我建议使用预处理语句或PDO
$carArray变量最终将包含carid作为键的数组
现在请注意,$pdo
是连接到数据库的pdo
对象,而不是mysqli
,您可以对mysqli做同样的事情,但我不习惯它
另外,我不建议在每一列上都使用用户名,而是应该将userId作为主键id的外键存储在user表中
这将节省更多的存储空间,使查询速度更快(查找数字比查找字符串更容易),用户可以更改其名称,而无需在所有其他表中更改其名称(当然,用户ID不会更改)
另外,carid应该是
无符号int(10)
而不是int(11)
如果想知道为什么10会看到post为了避免SQL注入,请使用预先准备好的语句。您可以使用一个查询获取车辆的所有属性:
$qry_id = "select carid, model, price, color from cars where user=?";
$stmt = mysqli_prepare($link , $qry_d) or die("SQL statement error");
// Bind the login parameter to the statement
mysqli_stmt_bind_param($stmt, "s", $login);
mysqli_stmt_execute($stmt);
// bind every column in the SELECT
mysqli_stmt_bind_result($stmt, $user_id, $carid, $model, $price, $color);
while (mysqli_stmt_fetch($stmt)){
$final_array[] = array(
"model" => $model,
"price" => $price,
"color" => $color
);
}
为什么选择数据只是为了重新选择数据?在一个循环中<代码>从用户=?的车辆中选择*或者,如果不是
*
,则选择所需的列列表。只需在一个查询中获取所需的所有数据。(还要注意,您有一个SQL注入漏洞。您需要查看带有查询参数的准备语句。)什么是$value\u user\u id
?您的代码中没有定义它。@David我将选择所有内容,但以后如何使用所需的值?在“$qry_model=”从carid=“$user_id”的汽车中选择模型“$valur\u user\u id
的值在每次迭代中都会改变,对吗?@trincot我已经改变了代码。我复制/粘贴了以前尝试的代码,但忘记更改该变量。@JoaoTorres:现在还不清楚您的意思或您试图在这里实现什么。您通过多个名称(汽车ID和用户ID)调用同一个值,那么这到底意味着什么呢?最后,当您到达这里时,您有哪些数据?您需要从数据库中获取哪些数据?我仍然不太明白为什么$login
变量在mysqli\u stmt\u bind\u param($stmt,“s”,$login)中起作用代码>?!我已经阅读了php网站上的文档,但仍然没有找到答案。SQL语句有一个?
,表示一个参数。SQL引擎可以编译这样的SQL语句,但在给出结果之前,您仍然需要告诉它该参数的值。您可以使用mysqli\u stmt\u bind\u param
执行此操作。s
表示您的参数是字符串类型$login
是参数值本身。这似乎比仅仅将$login
插入SQL字符串更复杂,但这是一个坏习惯,因为它会使代码容易受到SQL注入的攻击。
$q = "SELECT * FROM cars where user=?";
if ($stmt = $pdo->preapre($q)){
$result=[];
$stmt->execute([$login]);
while ($row = $stmt->fetchObject()){
$carId = $row->carid;
unset($row->carid);
$result[$carId]=$row;
}
}
$qry_id = "select carid, model, price, color from cars where user=?";
$stmt = mysqli_prepare($link , $qry_d) or die("SQL statement error");
// Bind the login parameter to the statement
mysqli_stmt_bind_param($stmt, "s", $login);
mysqli_stmt_execute($stmt);
// bind every column in the SELECT
mysqli_stmt_bind_result($stmt, $user_id, $carid, $model, $price, $color);
while (mysqli_stmt_fetch($stmt)){
$final_array[] = array(
"model" => $model,
"price" => $price,
"color" => $color
);
}