我有一张表格
http://s12.postimg.org/5lylvp2h9/Untitled.png
表产品
+----+---------------+
| id | product |
+----+---------------+
| 1 | pencil |
| 2 | Eraser |
| 3 | Brush |
| 4 | teddy bear |
| 5 | panda |
| 6 | kaleidoscope |
+----+---------------+
表供应商
+----+------------------+
| id | Suppliers |
+----+------------------+
| 1 | Silberauto , Ltd.|
| 2 | MADATA , Ltd. |
| 3 | Omnitel , Ltd. |
| 4 | Profito IT Ltd. |
| 5 | Arfix , Ltd. |
+----+------------------+
table product_supplier
+------------+-------------+-----------+
| product_id | supplier_id | unit_cost |
+------------+-------------+-----------+
| 4 | 1 | 10.50 |
| 2 | 5 | 15.25 |
| 1 | 2 | 20.40 |
| 3 | 3 | 25.75 |
| 5 | 3 | 30.00 |
| 5 | 4 | 35.75 |
| 1 | 1 | 40.55 |
| 4 | 3 | 45.75 |
| 2 | 2 | 50.15 |
| 3 | 5 | 55.10 |
| 6 | 2 | 60.00 |
| 9 | 2 | 65.55 |
| 7 | 4 | 70.85 |
| 9 | 3 | 75.35 |
| 8 | 1 | 80.20 |
+------------+-------------+-----------+
在我的表单选择字段中,我从表产品中选择一个项目( 铅笔 )。每个项目都有一个唯一值,该值引用了商品ID(表格产品 - > ID )。
在广播中选择我选择供应商(来自表供应商)。单选按钮具有值(在此示例中为MADATA,UAB
)和名称(在此示例中为项目ID 7
)
在下一个字段价格中,我想从表格unit_cost
打印product_suplier
product_id
(来自选择字段的value
。)等于supplier
(来自表suppliers
)。
kaina.php 我的情况
SELECT unit_cost
FROM product_supplier, suppliers
WHERE product_id= :product_id AND suppliers.suppliers = :suppliers');
$stmt->execute(array('supplier' => $supplier,'product_id' => $product_id));
while($row = $stmt->fetch()) {
echo $row['unit_cost']." LT ";
}
很糟糕。在此示例中,它打印两个值。如何将SELECT条件更改为一个值,而不是两个(因为有两个销售相同项目的供应商)。
我的代码:
的index.php
<?
$username='root';
$password='pass';
try {
$conn = new PDO('mysql:host=localhost;dbname=univer', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'KLAIDA: ' . $e->getMessage();
}?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<link rel='stylesheet' type= 'text/css' href='css/styles.css'>
<link rel='stylesheet' type= 'text/css' href='css/bootstrap.css'>
<script>
function tiekejai(tiekejas){
$.ajax({
type: "POST",
url: "tiekejai.php?q="+supplier,
dataType: "html",
success: function(duomenys){
$("#tiekejai").html(duomenys);
}
});
};
function kaina(tiekejas,data){
$.ajax({
type: "POST",
url: "kaina.php?supplier="+supplier+"&product_id="+data,
dataType: "html",
success: function(duomenys){
$("#kaina").html(duomenys);
}
});
};
</script>
</head>
<body>
<main id="content" role="content">
<div id="forma">
<form class="form-horizontal">
<fieldset>
<legend>Uzsakymo forma</legend>
<div class="control-group">
<label class="control-label" for="selectbasic">Pasirinkite preke</label>
<div class="controls">
<select id="selectas" name="selectas" class="input-xlarge"
onchange="tiekejai(this.value)">
<?php
$data = $conn->query('SELECT * FROM products
INNER JOIN product_suplier
ON products.id = product_suplier.product_id
GROUP BY product');
foreach($data as $row) {
echo "<option value=".$row['product_id'].">".$row['product']."</option>";
}
?>
</select>
</div><!--.controls -->
</div><!--.control-group -->
<div class="control-group">
<label class="control-label" for="radios">Pasirinkite tiekeja</label>
<div class="controls" id="tiekejai">
</div>
</div>
<div class="control-group">
<label class="control-label" for="radios">kaina</label>
<div class="controls">
<span class="uneditable-input" id="kaina">Prekes kaina</span>
</div>
</div>
<div class="control-group">
<label class="control-label" for="textinput">Kiekis</label>
<div class="controls">
<input id="textinput" name="textinput" placeholder="max 40" class="input-xlarge" type="text">
</div>
</div>
<div class="control-group">
<label class="control-label" for=""></label>
<div class="controls">
<button id="" name="" class="btn btn-primary">Uzsakyti</button>
</div>
</div>
</fieldset>
</div>
</main>
<script>
document.getElementById("selectas").selectedIndex = -1;
</script>
</body>
</html>
tiekejai.php
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', 'pass');
if (!$con){
die('Could not connect: ' . mysql_error());}
mysql_select_db("univer", $con);
$sql=" SELECT * FROM suppliers
INNER JOIN product_suppliers
ON tiekejai.id = product_suppliers.product_id
WHERE product_suppliers.product_id= '".$q ."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
echo "<label class='radio'>";
echo "<input name='". $q ."' id=radios
value= '". $row['supplier'] ."'
type='radio' onchange='kaina(this.value,this.name)'>";
echo $row['supplier'];
echo "</label>";
}
mysql_close($con);
?>
kaina.php
<?php
$supplier = $_GET['supplier'];
$product_id = $_GET['product_id'];
$username='root';
$password='pass';
try {
$conn = new PDO('mysql:host=localhost;dbname=univer', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('
SELECT unit_cost
FROM product_supplier, suppliers
WHERE product_id= :product_id AND suppliers.suppliers = :suppliers');
$stmt->execute(array('supplier' => $supplier,'product_id' => $product_id));
while($row = $stmt->fetch()) {
echo $row['unit_cost']." LT ";
}
}
catch(PDOException $e) {
echo 'KLAIDA: ' . $e->getMessage();
}
?>
答案 0 :(得分:0)
我认为从立陶宛语到英语的来回往往让你和我们感到困惑。我们现在可以用英语做整件事吗?所以数据集可能看起来像这样......
products
+----+---------------+
| id | product |
+----+---------------+
| 1 | pencil |
| 2 | Eraser |
| 3 | Brush |
| 4 | teddy bear |
| 5 | panda |
| 6 | kaleidoscope |
+----+---------------+
suppliers
+----+------------------+
| id | Suppliers |
+----+------------------+
| 1 | Silberauto , Ltd.|
| 2 | MADATA , Ltd. |
| 3 | Omnitel , Ltd. |
| 4 | Profito IT Ltd. |
| 5 | Arfix , Ltd. |
+----+------------------+
product_supplier
+------------+-------------+-----------+
| product_id | supplier_id | unit_cost |
+------------+-------------+-----------+
| 4 | 1 | 10.50 |
| 2 | 5 | 15.25 |
| 1 | 2 | 20.40 |
| 3 | 3 | 25.75 |
| 5 | 3 | 30.00 |
| 5 | 4 | 35.75 |
| 1 | 1 | 40.55 |
| 4 | 3 | 45.75 |
| 2 | 2 | 50.15 |
| 3 | 5 | 55.10 |
| 6 | 2 | 60.00 |
| 9 | 2 | 65.55 |
| 7 | 4 | 70.85 |
| 9 | 3 | 75.35 |
| 8 | 1 | 80.20 |
+------------+-------------+-----------+
注意:我删除了product_supplier表上的id列,因为它似乎没有用处。 现在,问题是什么?