如何从连接中涉及的一个表中获取记录的ID

时间:2015-11-06 12:27:40

标签: php html sql

我正在显示供应商及其产品的表格。

$productID = $_POST['simplexID'];

$suppliers = "SELECT * FROM suppliers_products, suppliers WHERE supplier_id = suppliers.id AND our_simp_code = '$productID'";
$supplierProducts = mysqli_query($connect , $suppliers)or die(mysqli_error($connect));

echo "<table>";
echo "<tr><th>Supplier</th><th>Pack Qty</th><th>Cost</th><th>Edit</th></tr>";
while ($row = mysqli_fetch_array($supplierProducts , MYSQLI_ASSOC)) {
    $id = $row['id'];
    echo "<tr><td>";
    echo $row['name'];
    echo "</td><td>";
    echo $row['pack_qty'];
    echo "</td><td>";
    echo $row['cost'];
    echo "</td><td>";
    echo "<a href='editsupplierproduct.php?id=$id' >Edit</a>";
    echo "</td></tr>";      
}
echo "</table>";

如您所见,表格中的最后一个数据字段是Edit链接,该链接将用户带到页面以编辑此记录。

事情是在表循环的第一行我获得该行的id但我的问题是它不知道哪个表从我使用连接获取id。 我怎么能克服这个?

编辑:这是编辑产品页面

 //Get id of product being edited
        $editID= $_GET['id'];

        $editProduct = "SELECT * FROM suppliers_products WHERE id = $editID";
        $product = mysqli_query($connect , $editProduct)or die("Error retrieving data!");

        echo "<div class='editsupproduct'>";
            echo "<form method='post' action='updatesupplierproduct.php'>";

                while ($row = mysqli_fetch_array($product , MYSQLI_ASSOC)) {
                    echo "Product Code: <input type='text' name='procode' value='" . $row['sup_product_code'] . "'><br>";
                    echo "Simplex Code: <input type='text' name='simcode' value='" . $row['our_simp_code'] . "'><br>";
                    echo "Pack Quantity: <input type='text' name='qty' value='" . $row['pack_qty'] . "'><br>";
                    echo "Cost: <input type='text' name='cost' value='" . $row['cost'] . "'>";
                    echo "<input type='hidden' name='proid' value='$editID'>";
                }

                echo "<input type='submit' value='Update'>";
            echo "</form>";

            echo "<form method='post' action='deletesupplierproduct.php'>";
                echo "<input type='hidden' name='supproduct' value='$editID'>";
                echo "<input type='submit' value='Delete'>";
            echo "</form>";


        echo "</div>";

1 个答案:

答案 0 :(得分:0)

'id'必须是唯一的,否则SQL会摇摆不定。

从你的表中看起来'id'只存在于'供应商'中 - 所以它就是那个。

如果您想要返回特定的“id”,那么您可以在查询中实现此目的:

SELECT suppliers_products.supplier_id as id,
       suppliers.* 
FROM suppliers_products, suppliers 
WHERE....

这将专门从'suppliers_products'表中返回'supplier_id',但别名'id'