php - localhost 将您重定向太多次 ERR_TOO_MANY_REDIRECTS
问题描述
你好堆栈溢出社区,我是一名学生,开始探索 php 的世界。我遇到过这种错误,如果我的代码有错误或没有错误,我不知道会发生什么
localhost 将您重定向了太多次。尝试清除您的 cookie。ERR_TOO_MANY_REDIRECTS
这是我的代码:(提前谢谢你们^_^
<?php
$error = "";
$db = mysqli_connect("localhost","root","","dbtuts");
if (isset($_GET['del'])) {
$id = $_GET['del'];
mysqli_query($db, "DELETE FROM tbl_links WHERE id=$id");
$_SESSION['message'] = "Address deleted!";
$error =" this is error";
}
?> <div class="alert alert-sucess">
<p>Sucessfully deleted!</p>
</div>
<?php header('Location: index.php');
exit;
?>
<!DOCTYPE html>
<html>
<head>
<title>uploading url links to sql</title>
</head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
<body>
<script>
function validateForm() {
var x = document.forms["myForm"]["links"].value;
var y = document.forms["myForm"]["notes"].value;
if (x == "" || x == null || y == "" || y == null) {
alert(" All field must be filled out");
return false;
}
}
</script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<div class="w3-card-4">
<div class="w3-container w3-brown">
<h2>ADD LINKS</h2>
</div>
<form class="w3-container" name="myForm" onsubmit="return validateForm()" action="add.php" method="POST" required/>
<p>
<label class="w3-text-brown"><b>Paste your URL here</b></label>
<input class="w3-input w3-border w3-sand" id = "a" name="links" type="text"></p>
<p>
<label class="w3-text-brown"><b>Notes</b></label>
<input class="w3-input w3-border w3-sand" id = "b" name="notes" type="text"></p>
<p>
<button class="w3-btn w3-brown" value="submit">Add to Database</button></p>
</form>
</div>
<h2>HTML Table</h2>
<table>
<tr>
<th> URL LINKS </th>
<th> DATE AND TIME </th>
<th> NOTES </th>
<th> ACTION </th>
</tr>
<?php
$sql="SELECT * FROM tbl_links";
$result_set=mysqli_query($db,$sql);
while($row=mysqli_fetch_array($result_set))
{
?>
<tr>
<td><a href = <?php echo $row['links'] ?> > <?php echo $row['links'] ?> </a></td>
<td><?php echo $row['date'] ?></td>
<td><?php echo $row['notes'] ?></td>
<form>
<td>
<a href="index.php?del=<?php echo $row['id']; ?>" class="del_btn">Delete</a>
</td>
</form>
</tr>
<?php
}
?>
</table>
</body>
</html>
解决方案
当您启动页面时,即使它没有删除您总是调用的任何内容
<?php header('Location: index.php');
所以网站没有机会启动。仅在删除时进行调用,如您所愿
<?php
$error = "";
$db = mysqli_connect("localhost","root","","dbtuts");
if (isset($_GET['del'])) {
$id = $_GET['del'];
mysqli_query($db, "DELETE FROM tbl_links WHERE id=$id");
$_SESSION['message'] = "Address deleted!";
$error =" this is error";
?>
<div class="alert alert-sucess">
<p>Sucessfully deleted!</p>
</div>
<?php header('Location: index.php');
exit;
}
?>
您的代码仍然容易受到sql 注入的影响,并且应该紧急切换到 带参数的准备好的语句,请参阅如何防止 PHP 中的 SQL 注入?
所以使用更好
<?php
$error = "";
$db = mysqli_connect("localhost","root","","dbtuts");
if (isset($_GET['del'])) {
$id = $_GET['del'];
if ($stmt = mysqli_prepare($link,"DELETE FROM tbl_links WHERE id= ?") {
mysqli_stmt_bind_param($stmt, "i", $id);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
$_SESSION['message'] = "Address deleted!";
$error =" this is error";
}
?>
<div class="alert alert-sucess">
<p>Sucessfully deleted!</p>
</div>
<?php header('Location: index.php');
exit;
}
?>
推荐阅读
- reactjs - React-Router 的 Redirect 在我使用时会引发 webpack 错误
- windows - OpenGL 中的 GL_CLAMP 与 GL_CLAMP_TO_EDGE 有何不同?
- c# - 使用来自 WCF 服务的数据填充 ASP.NET MVC 中的组合框
- reactjs - React-Table 不显示来自 Firebase 的数据
- react-native - 无法滚动 TabViewAnimated 视图
- windows - 在 Virtualbox 中启动 ubuntu 时 VT-x 不可用 (VERR_VMX_NO_VMX)
- flutter - 在 null 上调用了方法“[]”。(如何从 Firestore 中检索数据?)
- ios - Xcode 图像显得非常小
- android - 如何在销毁应用程序时保存媒体播放器数据?
- javascript - 使用 angular-router 在页面中导航视图