Dan Vincent Dan Vincent - 4 months ago 18
SQL Question

Can someone help me with this error " Warning: Cannot modify header information - headers already sent by"

this is the login form....it says that it cannot modify header information and I tried everything to fix but I cant...can someone help me with my codes..thanks in advance

<?php


$loginpopup = 'Login Success';
$failpopup = 'Wrong Username or Password';
if(!$con)
{
die("Error connection" . mysqli_connect_error());

}
if (isset($_POST['submitlogin']))
{

$login = "SELECT * from admin where username = '$_POST[user]' AND password ='$_POST[pass]'";

$getuser = mysqli_query($con,$login) or die(mysql_error());
while($row = mysqli_fetch_array($getuser))
{
if($row==0)
{
echo "<SCRIPT>alert('$failpopup');</SCRIPT>";
header("location:index.php");
}
else
{
echo "<SCRIPT>alert('$loginpopup');</SCRIPT>";
header("location:home.php");
}
}
}
?>

Answer

if the query failed to run your script will Die because of or die(mysql_error); which won't output anything after $getuser

modify your code to this

 include('movieshub/includes/config.php');
    if ($getuser = mysqli_query($con,$login)) { // check if the query succeeded running
      $count = mysqli_num_rows($getuser);
      if ($count == 0 ) {

             echo "<SCRIPT>alert('$failpopup');</SCRIPT>";
             header("location:index.php");

      } else  {

        while($row = mysqli_fetch_array($getuser)) 
        { //output data }
          echo "<SCRIPT>alert('$loginpopup');</SCRIPT>";
          header("location:home.php");
        }
    }
 } else {
echo "query failed to run";
 }