我正在创建我的第一个php应用程序,并遇到了有关如何尝试和编辑sql条目的问题。我正计划尝试弹出一个模式窗口以进行编辑。
当我选择编辑链接以打开模式窗口时,变量表示未定义。我认为这是因为在单击链接进行编辑之前,模态和变量已添加到DOM,并且变量从未设置。我该如何解决?
<?php
$sql = "SELECT id, image, name, score, points FROM teams";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)){
$teams_id = $row["id"];
$teams_image = $row["image"];
$teams_name = $row["name"];
$teams_score = $row["score"];
$teams_points = $row["points"];
// echo "id: " . $teams_id . " image path: " . $teams_image . " name: " . $teams_name . " score " . $teams_score . " points " . $teams_points . "</br>";
echo "<div class='row'>";
echo "<div class='row oval'>";
echo "<div class='col-4 flag {$teams_image}'>";
echo "</div>";
echo "<div class='col-8 text'>";
echo "<p>{$teams_name}</p>";
echo "</div>";
echo "</div>";
// edit and delete button
echo "<a href='add_teams_working.php?edit={$teams_id}' class='btn btn-outline-light btn-sm ml-4 align-self-center' data-toggle='modal' data-target='#editModal'>edit</a>";
echo "<button class='btn btn-outline-light btn-sm ml-1 align-self-center'>delete</button>";
echo "</div>";
}
}else {
echo "No teams added yet";
}
?>
<!-- SET VARIABLE for MODAL -->
<?php
if(isset($_GET['edit'])) {
$edit_id = $_GET['edit'];
$query = "SELECT * FROM teams WHERE id = $edit_id ";
$select_team = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($select_team)) {
$edit_id = $row['id'];
$edit_image = $row['image'];
$edit_name = $row['name'];
}
}
?>
如果需要的话,这里是模态的。
<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h3>Edit <?php echo $edit_name; ?> </h3>
</div>
<div class="modal-body">
<?php echo $edit_name; ?>
<?php echo $edit_image; ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button class="btn btn-primary" type="button">Save Changes</button>
</div>
</div>
</div>
</div>