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>
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();
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');
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']))
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.
I believe there are two problems.
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 .
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.