简体   繁体   English

根据用户的选择在while循环中获取变量?

[英]Get variable in while loop based on what user chooses?

I want to delete a single post from deleteupload.php hence I want to access product_id which is the unique identifier of each row after submission. 我想从deleteupload.php中删除一个帖子,因此我想访问product_id,这是提交后每一行的唯一标识符。 I have wrapped every product in a form element and a submit button. 我已经将每个产品包装在一个表单元素和一个提交按钮中。 So the page has multiple form elements. 因此页面具有多个表单元素。

This is my code: 这是我的代码:

 while ($row = mysql_fetch_array($loop))
 {
  $proID = $row['product_id'];

 echo "<table border='0' width='100%' align='center' class='centrebox'><tr>
 <td width='25%'>".($row['product_name']) . "</td> " . "
 <td width='35%'>".($row['product_disc'])."</td>";

 echo '<td width="40%"><img width="100%" height="300"  src="data:image/jpeg;base64,'.base64_encode( $row['product_image'] ).'"/></td></tr>
 <td><form method="post" action="deleteupload.php">
 <input type="submit" name="$proID" value="Delete"></form></td></table><br>';

}

This while loop iterates through the sql table and echos the columns (product_name, product_disc, and product_image). 该while循环遍历sql表并回显列(product_name,product_disc和product_image)。

You could put this in your html/php 您可以将其放在您的html / php中

       echo '<td width="40%"><img width="100%" height="300"  src="data:image/jpeg;base64,'.base64_encode( $row['product_image'] ).'"/></td></tr>
             <td><form name="form' . $proID . '" class="del_forms" method="post" action="deleteupload.php">
             <input type = "hidden" name="del_item" value="' . $proID .'" />
             <input type="submit" value="Delete" /></form></td></table><br>';

and in your deleteupload.php you would pick up the $proID; 在您的deleteupload.php您将获得$proID; as a $_POST['del_item']; 作为$_POST['del_item']; variable. 变量。 Assuming $proID; 假设$proID; is the ID number of the item you want to have deleted. 是您要删除的项目的ID号。

Note that you should be using mysqli not mysql as mysql is regarded as vulnerable to attack. 请注意,您应该使用mysqli而不是mysql因为mysql被认为容易受到攻击。 You should also escape/sanitize the $_POST variable to help prevent inject attack. 您还应该转义/清除$_POST变量,以帮助防止注入攻击。

  (int) $_POST['del_item'];

should achieve that in this case as long as the IDs are already integers. 只要ID已经是整数,就应该在这种情况下实现这一点。 (Won't work if they contain letters.) (如果它们包含字母,将不起作用。)

You might find this of interest as well: Multiple Forms or Multiple Submits in a Page? 您可能还会发现感兴趣的内容: 页面中是否包含多个表单或多个提交?

<table border='0' width='100%' align='center' class='centrebox'>
<?php 
 while ($row = mysql_fetch_array($loop))
 { 
  $proID = $row['product_id'];
?>
 <tr>
 <td width='25%'><?php ($row['product_name'])?></td> 
 <td width='35%'><?php ($row['product_disc']) ?></td>
 <td width='35%'>
    <a href="deleteupload.php?action=delete&id=<?php echo $proID ?>"></a>   
 </td>

<?php } ?>
</table>

**deleteupload.php**

<?php
if($_GET['action']=='delete'){

    delete query
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM