Skip to content
Advertisement

localhost redirected you too many times ERR_TOO_MANY_REDIRECTS

Hello stack overflow community, I’m a student and start to explore the world of php. I have experienced this kind of error and I have no idea what’s going on if my code have an error or haven’t

localhost redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS

Here’s my code 🙁 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>

Advertisement

Answer

When you start your page even when it do not delete anything you always call

<?php header('Location: index.php'); 

So the site has no chance to start. Do the call only when deleting, like you intended

<?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;
}
?>

Still your code is vulnurable to sql injection a and should urgently switch to prepared statements with parameters see How can I prevent SQL injection in PHP?

So use better

<?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;
}
?>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement