Mysql 基于id的SQL选择
您也不应该通过preg_replace“[^0-9]i”和$\u GET['id']来完成自己的工作,您应该参数化查询并使用PDO准备查询,并以$\u GET['id']作为输入执行查询。这将为您清理输入。为什么要用PDO标记它?嗨,Martin Perry,我已经编辑了它以显示完整的代码。。。我不知道是否有可能这样做,或者我必须将其全部添加到一个表中。嗨,Marty Perry,这很好,但我希望这是一种改变名称的方法。根据用户在上一页中选择的内容,使用php echo。我不太明白。。您可以在查询中使用变量,就像使用$id一样……我有一个名为product_list的页面,在该页面上我将数据库中的所有内容都回显到该页面。当您单击它时,它会根据您想要查看的产品显示product.php。上面的代码是针对该product.php上的product.php的,我有一个选项,根据用户从product_list.php页面选择的内容,我应该从数据库中删除一个列表。不,所有产品都有相同的选项,这就是为什么我想从数据库中获取每个产品选项的列表,我希望它们是一种你可以说选择t2的方式。从t2连接t2.vid上的t1=t1.id,其中t1。id@JamesBrown您可以使用字符串连接Mysql 基于id的SQL选择,mysql,pdo,Mysql,Pdo,您也不应该通过preg_replace“[^0-9]i”和$\u GET['id']来完成自己的工作,您应该参数化查询并使用PDO准备查询,并以$\u GET['id']作为输入执行查询。这将为您清理输入。为什么要用PDO标记它?嗨,Martin Perry,我已经编辑了它以显示完整的代码。。。我不知道是否有可能这样做,或者我必须将其全部添加到一个表中。嗨,Marty Perry,这很好,但我希望这是一种改变名称的方法。根据用户在上一页中选择的内容,使用php echo。我不太明白。。您可以在查
<!DOCTYPE html>
<head>
</head>
<body>
<?php
if (isset($_GET['ProductGroupNo'])) {
// Connect to the MySQL database
dbconnect();
$id = preg_replace('#[^0-9]#i', '', $_GET['ProductGroupNo']);
// To check to see if this ID exists, if yes then get the product
// details, if no then exit this script and give message why
$stmt = $conn->prepare("SELECT * FROM Product_Group WHERE ProductGroupNo=:id");
$stmt->bindParam('id',$id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($row) {
$Name = $row["Name"];
$Start_Cost = $row["Start_Cost"];
$Description = $row["Description"];
?>
<!-- Main Content -->
<div id="templatemo_body_wrapper">
<div id="templatemo_wrapper">
<div id="templatemo_main">
<div id="content" class="float_r">
<h4><?php echo $row['Name']; ?></h4>
<h5>Product Description</h5>
<p><?php echo $row['Description']; ?> </p>
<div class="cleaner h50"></div>
<div class="content_half float_r">
<table>
<tr>
<td width="160">Price:</td>
<td><?php echo $row['Start_Cost']; ?></td>
</tr>
<tr>
</tr>
<tr>
<td>Length(inches):</td>
<td>
<select name="length" id="length">
<?php
dbconnect();
$stmt = $conn->prepare("
SELECT Table2.Name
FROM Table2
LEFT JOIN Table1 ON Table1.vid = Table2.vid
WHERE Table1.id = '$id'");
$stmt->execute();
$i = 0;
foreach( $stmt->fetchAll(PDO::FETCH_ASSOC) as $row )
{
if ($i == 0)
{
echo '<option',fillSelect('Name',$row['vid'],'',true),' value="'.$row['vid'].'">'.$row['Name'].'</option>';
}
else
{
echo '<option',fillSelect('Name',$row['vid']),' value="'.$row['vid'].'">'.$row['Name'].'</option>';
}
$i++;
}
?>
</select>
------------------------------------------------------------------------------------------
**Table 1
id Vid coke Tea
1. 1 11 33
2. 2 32 44
Table 2
vid id Name snacks
1. 1 coke chocolate
2. 2 tea biscuit**
Is they a way to `SELECT` everything from the Name column if the $id matches the id from the table 1
SELECT Table2.Name
FROM Table2
LEFT JOIN Table1 ON Table1.vid = Table2.vid
WHERE Table1.id = '$id';
SELECT t2.Name FROM t2 JOIN t1 ON (t2.vid = t1.id)
WHERE t1.id = ?