大家好stack overflow社区,我是一名学生,开始探索php的世界。我经历过这样的错误,我不知道是怎么回事,如果我的代码有一个错误或没有。
localhost重定向您太多次。尝试清除您的cookies。ERR_TOO_MANY_REDIRECTS(重定向)
这是我的代码:( THANK YOU IN ADVANCE GUYS ^_^。
<?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>
当你启动你的age,即使当它不删除任何东西,你总是调用你的age
<?php header('Location: index.php');
所以网站没有机会启动。只有在删除的时候才做调用,就像你在tnede中一样
<?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;
}
?>