我要做的是将值传递给变量,具体取决于用户按下的按钮。我创建了一个带有while循环的表和删除按钮,但是,因此,所有按钮都具有相同的名称和ID。如何在按下相应按钮时将特定UserID传递给变量?
这是我的带嵌入式HTML的PHP
<?php
$servername = "localhost";
$username = "root";
$password = "A1128857795!";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else{
echo "Connected successfully";}
mysqli_select_db($conn,'test_schema');
$sql = "SELECT * FROM userinfo;";
$retvalues = mysqli_query($conn, $sql);
if(!$retvalues)
{
die("could not get data: " . mysqli_error());
}
print("<br>");
echo "<form action='delete.php' method='POST'>";
echo"<table border='1'>";
echo"<tr align='center'>";
echo "<th>ID</th>";
echo "<th>NAME</th>";
echo "<th>GENDER</th>";
echo "</tr>";
while($row = mysqli_fetch_array($retvalues, MYSQL_ASSOC))
{
print("<tr>");
echo "<td><input type='text' name='id' value='{$row['UserID']}'/></td>". " " . "<td>{$row['FirstName']}" . " " . "{$row['LastName']}</td>" .
" " . "<td align='center'>{$row['gender']}</td>";
echo "<td><input name='Delete' value='Delete' type='submit'></td>";
print("</tr>");
}
echo "</table>";
echo "</form>";
当我按下任何按钮时,它每次都返回表格最后一行的ID,导致DELETE语句始终从我的数据库中删除最后一条记录。
if(isset($_POST['id']))
{
$delUID = $_POST['id'];
$sql="DELETE FROM userinfo WHERE UserID = $delUID";
$query = mysqli_query($conn, $sql);
echo "DELETE FROM userinfo WHERE UserID = $delUID;";
}
$conn->close();
?>
欢迎任何建议。谢谢。
答案 0 :(得分:1)
您可以在旧学校输入按钮的名称中使用数组,也可以使用<button>
标记来设置不同的值和标题。
<form method="post">
<?php for($id = 1; $id <= 3 ; $id++): ?>
<input type="submit" name="delete_a[<?php echo $id; ?>]" value="Caption of delete button a">
<button type="submit" name="delete_b" value="<?php echo $id; ?>">Caption of delete button b</button>
<br>
<?php endfor; ?>
</form>
<?php
var_dump($_POST);
?>
使用数组版本,您甚至可以使用复选框和任何提交按钮一次删除多行。然后,您对数组$_POST['delete_a']
的键感兴趣。
答案 1 :(得分:0)
我会考虑做这样的事情
<?php
$servername = "localhost";
$username = "root";
$password = "A1128857795!";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else{
echo "Connected successfully";}
mysqli_select_db($conn,'test_schema');
$sql = "SELECT * FROM userinfo;";
$retvalues = mysqli_query($conn, $sql);
if(!$retvalues)
{
die("could not get data: " . mysqli_error());
}
print("<br>");
echo "<form action='delete.php' method='POST'>";
echo"<table border='1'>";
echo"<tr align='center'>";
echo "<th>ID</th>";
echo "<th>NAME</th>";
echo "<th>GENDER</th>";
echo "</tr>";
while($row = mysqli_fetch_array($retvalues, MYSQL_ASSOC))
{
print("<tr>");// I don't think u need to give user a chance to edit their UserID.. so I kept it in hidden type
echo "<td>{$row['UserID']}<input type='hidden' name='userid' value='{$row['UserID']}'/></td>". " " . "<td>{$row['FirstName']}" . " " . "{$row['LastName']}</td>" .
" " . "<td align='center'>{$row['gender']}</td>";
echo "<td><input name='Delete' value='Delete' type='submit'></td>";
print("</tr>");
}
echo "</table>";
echo "</form>";
接收方
if(isset($_POST['Delete']))
{
$delUID = $_POST['userid'];
$sql="DELETE FROM userinfo WHERE UserID = $delUID";
$query = mysqli_query($conn, $sql);
echo "DELETE FROM userinfo WHERE UserID = $delUID;";
}
$conn->close();
答案 2 :(得分:0)
@f_anto有正确的想法。
使用查询循环创建表,但为每一行附加一个唯一的链接或按钮。
while($row = mysqli_fetch_array($retvalues, MYSQL_ASSOC)) {
echo "<td><input type='text' name='id' value='{$row['UserID']}'/></td>"
. "<td>{$row['FirstName']} " . "{$row['LastName']}</td>" .
. "<td align='center'>{$row['gender']}</td>"
. "<td><a href='' id='$row['UserID']' class='delete-button'></td>";
}
然后使用AJAX调用进行肮脏的工作。
$(".delete-button").on('click', function(e){
e.preventDefault();
var $id = $(this).attr("id");
$.ajax({
type: "POST",
url: "delete.php",
data: ["id": $id]
});
});