为什么我的“删除”按钮不起作用? 行(36,134)
这里是代码:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users";
$con = mysqli_connect("localhost","root","");
if (!$con){
die("can not connect:" . mysql_error());
}
$db_select = mysqli_select_db($con, "users");
if (!$db_select) {
die("Database selection failed: " . mysqli_error($con));
}
// IF ID WIll post
if(isset($_POST['ID'])){
$query = "UPDATE `employees`
SET `Name` = '".$_POST['name']."',
`Last Name` = '".$_POST['lastname']."',
`Birth Date` = '".$_POST['birth']."',
`Position` = '".$_POST['position']."',
`Job Applied` = '".$_POST['applied']."'
WHERE
`ID` = '".$_POST['ID']."'
";
$con->query($query);
header("Location: http://localhost/barami.php");
exit;
}
if(isset($_GET['removeID'])){
$query = "DELETE
FROM
`employees`
WHERE
(`ID` = '".$_POST['removeID']."')";
$con->query($query);
header("Location: http://localhost/barami.php");
exit;
}
if(isset($_GET['ID'])){
$sql = "SELECT * FROM `Employees` WHERE ID='".$_GET['ID']."'";
$CurrentInfo = $con->query($sql);
if ($CurrentInfo->num_rows > 0) {
$info = mysqli_fetch_assoc($CurrentInfo);
?>
<form action="http://localhost/barami.php" method="post">
<!-- POST-in ID -->
<input type="hidden" name="ID" value="<?php echo $info["ID"];?>">
<!-- POST-in ID -->
<input type="text" name="name" placeholder="Name" value="<?php echo $info["Name"];?>">
<input type="text" name="lastname" placeholder="Last Name" value="<?php echo $info["Last Name"];?>">
<input type="text" name="birth" placeholder="Birth Date" value="<?php echo $info["Birth Date"];?>">
<input type="text" name="position" placeholder="Position" value="<?php echo $info["Position"];?>">
<input type="text" name="applied" placeholder="Job Applied" value="<?php echo $info["Job Applied"];?>">
<input type="submit" value="+">
</form>
<?php
}
}else{ ?>
<form action="http://localhost/barami.php" method="post">
<input type="text" name="name" placeholder="Name" >
<input type="text" name="lastname" placeholder="Last Name" >
<input type="date" name="birth" placeholder="Birth Date" >
<input type="text" name="position" placeholder="Position" >
<input type="text" name="applied" placeholder="Job Applied" >
<input type="submit" value="+">
</form>
<?php
}
if(isset($_POST['name'], $_POST['lastname'], $_POST['birth'], $_POST['position'], $_POST['applied'])){
$name = htmlentities($_POST['name'], ENT_QUOTES, 'UTF-8');
$lastname = $_POST['lastname'];
$birth = $_POST['birth'];
$position = $_POST['position'];
$applied = $_POST['applied'];
if(!(strlen($name) > 0)){
die('enter $name');
if(!(strlen($lastname) > 0)){
die('enter $lastname');
}
if(!(strlen($position) > 0)){
die('enter $position');
}
if(!(strlen($applied) > 0)){
die('enter $applied');
}}
$sql = "INSERT INTO `Employees` (`Name`, `Last Name`, `Birth Date`, `Position`, `Job Applied`) VALUES ('".$name."', '".$lastname."', '".$birth."', '".$position."', '".$applied."')";
mysqli_query($con, $sql);
header("Location: http://localhost/barami.php");
die();
}
$sql = "SELECT * FROM `Employees`";
$result = $con->query($sql);
echo "<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Birth Date</th>
<th>Position</th>
<th>Job Applied</th>
</tr>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>" . $row["Name"]. "</td>
<td>" . $row["Last Name"]. "</td>
<td>" . $row["Birth Date"]. "</td>
<td>" . $row["Position"]. "</td>
<td>" . $row["Job Applied"]. "</td>
<td><a href='http://localhost/barami.php?ID=" . $row["ID"]. "'>EDIT</a></td>
<td><a href='http://localhost/barami.php?removeID=" . $row["ID"]. "'>DELETE</a></td>
</tr>
";
} echo "</table>";
} else {
echo "0 results";
}
?>
<style>
table {
width: 50%;
}
td, th {
border: 1px solid grey;
text-align: left;
padding: 8px;
border-radius: 8px;
}
</style>
答案 0 :(得分:0)
如果存在带有removeID isset($_GET['removeID'])
的GET请求,则您正在输入条件,但是,您正在尝试读取POST变量(
ID = '".$_POST['removeID']."')";
您需要将其更改为$_GET['removeID']