![](/img/trans.png)
[英]How do I close the modal window in react when clicking on the Add product button?
[英]How do i add a button that on clicking will take me to the product page with more product description along with comments and rating
我正在嘗試為 uni 做一個項目,但被困在這一特定部分。 我正在嘗試顯示一個按鈕,該按鈕可以打開特定產品並讓用戶訂購/添加到購物車/評價它
我幾乎對 Java 腳本知之甚少,大約 2 個月前開始使用 PHP。 這是我的代碼。
$sql="select * from products";
$rs=$db->query($sql);
echo "<form method='post' action=viewitem.php>";
echo "<table>";
foreach ($rs as $row) {
$id=$row[0];
echo "<tr><td><img src = '/3331/333/bootstrap-shop/pimages/MensShirts/$row[6]' width='250' height='250'></td>
<td><pre>$row[5]</pre></td></tr>";
echo "<tr><td>$row[3] - <strong>BD$row[4]</strong></td></tr>";
echo "<td><input type='submit' name='$id' value='View more'/></td></tr>";
}
echo "</table>";
echo "</form>";
我提交的表格是
$pid=$id;
$sql="select * from products where pid='$pid'";
$rs=$db->query($sql);
echo "<form method='post' action=addtocart.php>";
echo "<table>";
foreach ($rs as $row) {
$id=$row[0];
echo "<tr><td><img src = '/3331/333/bootstrap-shop/pimages/MensShirts/$row[6]' width='250' height='250'></td></tr>";
echo "<tr><td>$row[3] - <strong>BD$row[4]</strong></td></tr>";
}
echo "</table>";
echo "</form>";
我應該在那里添加更多項目,但我似乎無法找出用戶完全點擊的產品。 它一直說$pid/$id
未定義。
因此,無需提交表單來查看產品,只需創建一個鏈接並將產品 ID 作為參數傳遞即可。
例如,在您的列表頁面上,您將...
<?php $query = $db->query( "SELECT * FROM products" ); ?>
<table>
<?php foreach ( $query as $row ): ?>
<tr><td><img src="/3331/333/bootstrap-shop/pimages/MensShirts/<?php echo $row[ 6 ]; ?>" width="250" height="250"></td>
<td><pre><?php echo $row[ 5 ]; ?></pre></td></tr>
<tr><td><?php echo $row[ 3 ]; ?> - <strong>BD<?php echo $row[ 4 ]; ?></strong></td></tr>
<td><a href="viewitem.php?id=<?php echo $row[ 0 ]; ?>">View more</a></tr>
<?php endforeach ?>
</table>
具體<a href="viewitem.php?id=<?php echo $row[ 0 ]; ?>">View more</a>
單擊時,這會將用戶帶到類似於以下內容的 URL: http://example.com/viewitem.php?id=123
: http://example.com/viewitem.php?id=123
然后在viewitem.php
文件中,我們從 URL 中獲取id
參數並執行查詢。 例如:
<?php
$pid = !empty( $_GET[ 'id' ] ) ? (int)$_GET[ 'id' ] : null;
if ( $pid ) {
$query = $db->query( "SELECT * FROM products WHERE pid = {$pid}" );
?>
<form method="post" action="addtocart.php">
<table>
<?php foreach ( $query as $row ): ?>
<tr><td><img src="/3331/333/bootstrap-shop/pimages/MensShirts/<?php echo $row[ 6 ]; ?>" width="250" height="250"></td>
<tr><td><?php echo $row[ 3 ]; ?> - <strong>BD<?php echo $row[ 4 ]; ?></strong></td></tr>
<?php endforeach ?>
</table>
</form>
<?php
} else {
echo 'No product ID';
}
URL 參數可通過$_GET
數組訪問,但由於我們將在 SQL 查詢中使用此參數的值,因此我們必須清理數據,因為您很容易受到 SQL 注入攻擊。
下面一行基本上是檢查id
參數是否存在於 url 中,如果它確實確保它是一個有效的整數。
$pid = !empty( $_GET[ 'id' ] ) ? (int)$_GET[ 'id' ] : null;
這和...
$pid = null;
if ( !empty( $_GET[ 'id' ] ) ) {
$pid = (int)$_GET[ 'id' ];
}
現在舉一個簡單的例子,如果你不清理輸入數據會發生什么......
如果您將$_GET[ 'id' ]
值直接傳遞到 sql 查詢中,例如:
$db->query( "SELECT * FROM products WHERE pid = " . $_GET[ 'id' ] );
然后攻擊者可以開始在您的數據庫上運行他們自己的 SQL 查詢,因此如果他們訪問了http://example.com/viewitem.php?id=1; SELECT * FROM users;
http://example.com/viewitem.php?id=1; SELECT * FROM users;
將運行以下查詢...
SELECT * FROM products WHERE pid = 1; SELECT * FROM users;
這是 SQL 注入的一個非常基本的示例,我強烈建議對其進行更深入的研究。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.