0

Can I create something like that after running a query?

if($query)
{
  succes.php;
}
else
{
  fail.php;
}

Tried this but didn't work either..

$url = '../modul_produk/index.php';
$url2 = '../modul_produk/index2.php';
if($query)
{
    header('Location: ' . $url);
}
else
{
 header('Location: ' . $url2);
}

I get this error

Notice: Undefined variable: query in >C:\xampp\htdocs\Payang\modul_daftar\index4.php on line 148

Warning: Cannot modify header information - headers already sent by (output >started at C:\xampp\htdocs\Payang\modul_daftar\index4.php:126) in >C:\xampp\htdocs\Payang\modul_daftar\index4.php on line 153

line 126 is refer to ?php

line 148 and 153 both refer to the $url and the header..

What I don't get it is why is the ?php deem as a header? How can I fix this? Tried all of the mention answer and it seems the problem is at the ?php as mention in the error at line 126.

For the ref this is the full code:

<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="UTF-8"/>
        <title>Sistem Laporan Bersepadu</title>
        <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.5/darkly/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  <script>
  function uploadImage(oForm) {
  var _validFileExtensions = [".jpg", ".jpeg", ".gif", ".png"];
  var oInput = document.getElementById("image");
  var sFileName = oInput.value;
  var size = oInput.files[0].size;   
  
  var oFReader = new FileReader();
  oFReader.readAsDataURL(document.getElementById("image").files[0]);
  oFReader.onload = function(oFREvent) {
   document.getElementById("uploadPreview").src = oFREvent.target.result;
  };
  
  if (sFileName.length > 0) {
   var blnValid = false;
   for (var j = 0; j < _validFileExtensions.length; j++) {
    var sCurExtension = _validFileExtensions[j];
    if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
     blnValid = true;
     break;
    }
   }    
   if (!blnValid) {
    document.getElementById("alert").innerHTML="<span class='label label-danger'>Tidak Sah</span>";
    document.getElementById("submitBtn").disabled = true;
    return false;
   }
  }   
  if(size > 4294967295){
   document.getElementById("alert").innerHTML="<span class='label label-danger'>Tidak Sah</span>";
   document.getElementById("submitBtn").disabled = true;
   return false;
  }else {   
   document.getElementById("alert").innerHTML="<span class='label label-success'>Sah</span>.";
   document.getElementById("submitBtn").disabled = false;
   return true;    
  }
  }
  </script> 
</head>
<body>
 <div class="container">
 <div class="row">
  <div class="col-sm-2">
  </div>
  <div class="col-sm-8">
   <h2>Borang Pendaftaran</h2>
   <h5>Sila Isi Semua Maklumat</h2>
   
   <div class="panel panel-primary">
   <div class="panel-heading"><span class="glyphicon glyphicon-paperclip"></span> Borang untuk pelapor</div>
    <div class="panel-body">
     <form id="produkForm" action="../modul_kemaskini/index.php" class="form-horizontal" method="post" enctype="multipart/form-data">  
      <fieldset>          
                  
       
       <div class="form-group">
        <label for="inputKodProduk" class="col-lg-2 control-label">Kod Produk</label>
        <div class="col-lg-10">
         <input type="text" class="form-control" name="inputKodProduk" id="inputKodProduk" placeholder="KodProduk">
        </div>
       </div>
       
       <div class="form-group">
        <label for="inputNamaProduk" class="col-lg-2 control-label">Nama Produk</label>
        <div class="col-lg-10">
         <input type="text" class="form-control" name="inputNamaProduk" id="inputNamaProduk" placeholder="NamaProduk">
        </div>
       </div>
       
       <div class="form-group">
        <label for="inputSpesifikasi" class="col-lg-2 control-label">Spesifikasi</label>
        <div class="col-lg-10">
         <input type="text" class="form-control" name="inputSpesifikasi" id="inputSpesifikasi" placeholder="Spesifikasi">
        </div>
       </div>
      
         
       <div class="form-group">
         <label for="selectJenis" class="col-lg-2 control-label">Jenis</label>
         <div class="col-lg-10">
          <select class="form-control" id="selectJenis" name="selectJenis">
           <option value="silapilih">Sila Pilih</option>
           <option value="Telefon">Telefon</option>
           <option value="Tv">Tv</option>
           <option value="Laptop">Laptop</option>
          </select>
         </div>
       </div>
       
       <div class="form-group">
        <label for="inputHarga" class="col-lg-2 control-label">Harga</label>
        <div class="col-lg-10">
         <input type="text" class="form-control" name="inputHarga" id="inputHarga" placeholder="RM">
        </div>
       </div>
       
       <div class="form-group">
        <label for="image" class="col-lg-2 control-label">Gambar</label>
        <div class="col-lg-10">
         <img src="" id="uploadPreview" class="img-responsive" style="height: 100px;" />
         <input type="file" name="image" id="image" onchange="uploadImage(this)">
        </div>
       </div>
       
       
       
       <div class="form-group">
         <div class="col-lg-10 col-lg-offset-2"> 
          
          <input type="submit" name="submit" value="Save" />
         <div id="alert"></div>        
        </div>
       </div>
       
       <?php 
       if (isset($_POST['submit']))
       {    
        include '../include/conn.php';
                
        $kod=$_POST['inputKodProduk'] ;
        $namaproduk= $_POST['inputNamaProduk'] ;     
        $spesifikasi=$_POST['inputSpesifikasi'] ;
        $jenis=$_POST['selectJenis'] ;
        $harga=$_POST['inputHarga'] ;
        $image= addslashes($_FILES['image']['tmp_name']);
        $name= addslashes($_FILES['image']['name']);
        $image= file_get_contents($image);
        $image= base64_encode($image);
     
        $query = mysql_query("INSERT INTO `produk2`
        (product_code,product_name,product_desc,product_type,product_price,product_img,product_img_name) 
        VALUES ('$kod','$namaproduk','$spesifikasi','$jenis','$$harga','$image','$name')");
       }
       if($query)
{
    $url = '../modul_produk/index.php';
}
else
{
    $url = '../modul_produk/index2.php';
}

header('Location: ' . $url);
exit();
        
        
   
    
   
       ?>
       
       
      
    </div>
   </div>
  </div>
 </div>
 </div>

 
            
    
</fieldset>
</form>
</body>
</html>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mohd Fadli
  • 143
  • 10

5 Answers5

0

The error is because you are sending header twice in the same file. Try storing the URL in a variable using conditional statement and only setting the header once instead. Something like this might work:

if($query)
{
    $url = '../modul_produk/index.php';
}
else
{
    $url = '../modul_produk/index2.php';
}

header('Location: ' . $url);
exit();
Acharya Anurag
  • 671
  • 6
  • 23
  • Can you post the whole code of the page? There must be something else that's already sending the header information from the page. – Acharya Anurag Aug 23 '15 at 09:44
0

Is the url you are referring to a internal page or external page on Internet? if its a external page, then I would agree with @Rino Raj but if its a internal page, all you need is the header('Location:thepage.php');

koroush
  • 69
  • 1
  • 2
  • 9
  • its an internal page. and i already triend (location:page.php') but like the updated question, the problems is at line 126 that is php – Mohd Fadli Aug 23 '15 at 07:31
0

try the below code

$url = '../modul_produk/index.php';
$url2 = '../modul_produk/index2.php';
if($query)
{
    header("Location: ${url}");
}
else
{
 header("Location: ${url2}");
}

you need to do a little change in your code, move the

if($query)...

inside the

if(isset($_POST['session']))
Jawad Azzam
  • 49
  • 3
  • 15
  • tried it and still the same problems Notice: Undefined variable: query in C:\xampp\htdocs\Payang\modul_daftar\index4.php on line 146 Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\Payang\modul_daftar\index4.php:126) in C:\xampp\htdocs\Payang\modul_daftar\index4.php on line 154 – Mohd Fadli Aug 23 '15 at 07:51
0

First check your code at 148 what is undefined, please define it, if that is not possible , just do a ob_start() in top of the file , that should suppress the buffer header already sent warning, and should redirect you in desired page.

and also please add a exit() or die() after the header location code.

dip1232001
  • 240
  • 3
  • 10
0

I believe there are two problems.

  1. Calling the header() function sends HTTP headers. These contain some metadata about server request and instructs the browser what to do with the response, in your case display a different page. They must be sent as the first thing in the response, otherwise it's too late for the browser to interpret them.

    In your case, the server first sends <!DOCTYPE html>... and it's too late then for sending headers. You must call header() before any output is made. This includes sending HTML, calling echo ... or even sending error message (Notice: ... in your case`)

    What to do about it? The simplest solution is to move call to header() earlier in the script. Put

       <?php    
           if (isset($_POST['submit'])) {      
              include '../include/conn.php';
    
              $kod=$_POST['inputKodProduk'] ;
              //....
              $query = mysql_query("INSERT INTO `produk2`
                (product_code,product_name,product_desc,product_type,product_price,product_img,product_img_name) 
                VALUES ('$kod','$namaproduk','$spesifikasi','$jenis','$$harga','$image','$name')");
              if($query) {}
                $url = '../modul_produk/index.php';
              } else {
                $url = '../modul_produk/index2.php';
              }
              header('Location: ' . $url);
              exit();
           }
      ?>
    

    as the very first thing in your script. A comprehensive description how to deal with this problem can be found here.

  2. Another problem is the Notice: Undefined variable: query error. If isset($_POST['submit'])) is evaluated to false, variable $query is not initialized. You probably want to move your if($query) if block inside if(isset($_POST['submit']))) if block, as in the example I gave in above. Otherwise the notice message will be sent to the browser and it may mess up sending headers too.

Community
  • 1
  • 1
Mifeet
  • 12,949
  • 5
  • 60
  • 108