简体   繁体   中英

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?

-sorry for the spaggeti code

In the case of the code you provided, you could add hidden input elements in your forms.

Use the same name and set the value to the unique id you want. 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; $_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.

A couple of extra notes beyond the scope of the question;

1 . You are currently echoing the HTML you create in PHP before the <!DOCTYPE html> tag. Although a browser will correct for this, this isn't proper HTML form.

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.

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;

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

It is possible to interweave PHP and HTML like this in a PHP document.

2 . Although already mentioned in the comments of your question, here's an extra SQL Injection warning for people passing through in the future.

Warning : You are wide open to SQL Injections and should use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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