简体   繁体   English

如何从while循环中获取唯一的按钮ID? (PHP)

[英]How to get unique button id from a while loop? (PHP)

First of all here's my code:首先这是我的代码:

session_start();
require_once "config.php";
$email = $_SESSION["email"];
$result = mysqli_query($link,"SELECT * FROM cuponai WHERE gmail='$email' AND panaudoti=0");

echo "<table border='1'>
<tr>
<th>Cupono kodas</th>
<th>Apply code</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr> ";
echo "<td>" . $row['cuponas'] . "</td>";
echo "<td> <form method='post'><input type='submit' name='". $row['cuponas'] ."' value='submit'></from></td>";
echo "</tr>";
}
echo "</table>";
if(isset($_POST['???????'])) {
$sql = "UPDATE cuponai SET panaudoti=1 WHERE cuponas='????????????' ";

if ($link->query($sql) === TRUE) {
  echo "YOU DID IT WOO";
} else {
  echo "Error updating record: " . $link->error;
    }
}
mysqli_close($link);
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>

</body>
</html>

The parts in question marks is where I'm strugling... Basically I'm making this site where you can see your purchased coupons and each of the coupons have an apply button near them so you basically have a list and need to apply them after you apply them I want it too refresh the page and applied coupons dissapear and are marked as used in the database.问号中的部分是我苦苦挣扎的地方......基本上我正在制作这个网站,您可以在其中看到您购买的优惠券,每个优惠券附近都有一个应用按钮,因此您基本上有一个列表并需要应用它们应用它们后,我希望它也刷新页面,并且应用的优惠券消失并在数据库中标记为已使用。 But I've been scratching my head this whole time and trying to make the buttons unique with javascirpt and all that but to no avail hopefully some of you could figure out a solution here?但是我一直在摸不着头脑,并试图用 javascirpt 使按钮独一无二,但无济于事,希望你们中的一些人能在这里找到解决方案?

-sorry for the spaggeti code - 对不起意大利面代码

In the case of the code you provided, you could add hidden input elements in your forms.对于您提供的代码,您可以在 forms 中添加隐藏的输入元素。

Use the same name and set the value to the unique id you want.使用相同的名称并将值设置为您想要的唯一 ID。 This input's value and name will be sent to the PHP portion of your file where you can access it inside the $_POST array like so;此输入的值和名称将发送到文件的 PHP 部分,您可以在$_POST数组中访问它,如下所示; $_POST['name_of_input']

In short, you would have to make the following adjustments to your code to make it work as you expect;简而言之,您必须对代码进行以下调整以使其按预期工作;

Change this改变这个

echo "<td> <form method='post'><input type='submit' name='". $row['cuponas'] ."' value='submit'></from></td>";

To this对此

echo "<td> <form method='post'><input type='hidden' name='couponid' value='". $row['cuponas'] ."'><input type='submit' name='". $row['cuponas'] ."' value='submit'></from></td>";

And then change this然后改变这个

if(isset($_POST['???????'])) {
$sql = "UPDATE cuponai SET panaudoti=1 WHERE cuponas='????????????' ";

To this对此

if(isset($_POST['couponid'])) {
$sql = "UPDATE cuponai SET panaudoti=1 WHERE cuponas='".$_POST['couponid']."' ";

Your $_POST['couponid'] will hold the data of $row['cuponas'] and your code should work as you expect.您的$_POST['couponid']将保存$row['cuponas']的数据,您的代码应该可以按预期工作。

A couple of extra notes beyond the scope of the question;除了问题的 scope 之外,还有一些额外的说明;

1 . 1 . You are currently echoing the HTML you create in PHP before the <!DOCTYPE html> tag.您当前正在回显您在<!DOCTYPE html>标记之前在 PHP 中创建的 HTML。 Although a browser will correct for this, this isn't proper HTML form.尽管浏览器会对此进行纠正,但这不是正确的 HTML 形式。

Rather than echoing everything at the top of the document, you might want to store it in a variable and echo that inside the <body> tag instead.与其在文档顶部回显所有内容,不如将其存储在一个变量中并在<body>标记内回显它。

Instead of this;而不是这个;

echo "<table border='1'>
<tr>
<th>Cupono kodas</th>
<th>Apply code</th>
</tr>";

You could use;你可以使用;

$html_to_echo = "<table border='1'>
<tr>
<th>Cupono kodas</th>
<th>Apply code</th>
</tr>";

To add to this variable you can use something like this;要添加到这个变量,你可以使用这样的东西;

$html_to_echo .= "<tr> ";

And then in the HTML portion of your page;然后在页面的 HTML 部分;

<body>
    <?php echo $html_to_echo; ?>
</body>

It is possible to interweave PHP and HTML like this in a PHP document.可以像这样在 PHP 文档中交织 PHP 和 HTML。

2 . 2 . Although already mentioned in the comments of your question, here's an extra SQL Injection warning for people passing through in the future.尽管在您的问题的评论中已经提到过,但这里有一个额外的 SQL 注射警告,供将来经过的人使用。

Warning : You are wide open to SQL Injections and should use parameterized prepared statements instead of manually building your queries.警告:您对SQL 注入持开放态度,应该使用参数化的准备好的语句而不是手动构建查询。 They are provided by PDO or by MySQLi .它们由PDOMySQLi提供。 Never trust any kind of input, Even when your queries are executed only by trusted users, you are still in risk of corrupting your data .永远不要相信任何类型的输入,即使您的查询仅由受信任的用户执行,您仍然有损坏数据的风险 Escaping is not enough Escaping 不够用

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

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